Category Archives: parallel

C++11 Concurrency: Part 1 – Introduction to Threading in Standard Library

C++ is growing, as per new standard C++11 now it supports threading. C++11 introduce a new thread library. The new standard define utilities for starting and managing threads. It also contains utilities for synchronization like mutexes and other locks, atomic variables, and other utilities. We will get through this so called C++11 threading by series of articles “C++11 Concurrency”.

To compile the sample code, we need a C++11 compiler. In my case, I use GCC 4.8.1 (on Slackware Linux) or MinGW counterpart (on Windows 8.1). To activated C++11 support features, we need to pass the “-std=c++11″ option to GCC, for example:

g++ filename.cpp -o program -std=c++11

C++ Concurrency

see <atomic>, <condition_variable>, <future>, <mutex>, and <thread> for reference.

Starting from C++11, there are five more headers added to the standard library to support concurrency. They are:

  • atomic
  • condition_variable
  • future
  • mutex
  • thread

These headers are used for multithreading and concurrency. Before the language standard, we would use OS facilities such as POSIX thread or libraries like OpenMP and MPI to enable concurrency using C++.

The <atomic> header defines some definition to allow us using atomic operation. It encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory access among different threads.

The <condition_variable> header defines classes used as condition variable for synchronization.

The <future> header facilitates synchronization access to value.

The <mutex> header is used for allowing mutual exclusion (mutex) of concurrent execution of critical sections of code.

The <thread> header defines thread class, used for spawning threads on C++ and also manage threads as we like.

Starting Threads

see also: Multithreading Support in C++11

Starting a new thread is very easy. We need to create an instance of a std::thread and supply a function and thread will automatically be started and executing the function. The function we talk about can be a function pointer or even a lambda expression.

So first, let’s write a simple code to demonstrate the power of C++ thread.

#include <thread>
#include <iostream>

using std::cout;
using std::endl;
using std::thread;

void hello(){
    cout << "Hello from thread " << endl;
}

int main(){
    thread t1(hello);
    t1.join();

    return 0;
}

In the above snippet, we pass a function pointer to thread.

All the threads utilities can be found in the <thread> header. As we see, when constructing a thread instance, we supply a function which will be executed on the fly. The join() method is called to force current thread to wait for the other one. In other word, main thread won’t finish until t1 finish it’s task. If we omit this call, the result is undefined.

We can, however, create more than one thread. To distinguish each of thread, the std::thread class provide a get_id() method which returns an unique id for this thread. We can get a reference to the current thread by std::this_thread variable. Let’s dive in example:

#include <thread>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::thread;
using std::vector;

void hello(){
    cout << "Hello from thread " << this_thread::get_id() << endl;
}

int main(){
    vector<thread> threads;

    for (int i = 0; i < 5; ++i){
        threads.push_back(thread(hello));
    }

    for (auto& thread : threads){
        thread.join();
    }

    return 0;
}

Sample output:

Hello from thread 2
Hello from thread 4
Hello from thread 3
Hello from thread 5
Hello from thread 6

Our program will start thread one after one and then store them into a vector. This is common strategy to handle several threads.

The threads might interleave. We have no way to control the order of execution of threads. A thread can be preempted at any moment which might lead us to produce following result:

Hello from thread Hello from thread 6
Hello from thread 3
Hello from thread 5
2
Hello from thread 4

So, there is no certainty in which function will be executed first. All decision are taken by OS. The Operating System will decide when resource are available for our threads and execute them on whatever CPU is least occupied.

Aside from function pointer, we can also use lambda expression. Lambda execution is very useful when the executing code is very small thus we don’t necessary need to create a function just for that. We can rewrite the previous code as this one:

#include <thread>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::thread;
using std::vector;

int main(){
    vector<thread> threads;

    for (int i = 0; i < 5; ++i){
        threads.push_back(thread([]() {
            cout << "Hello from thread " << this_thread::get_id() << endl;
        }));
    }

    for (auto& thread : threads){
        thread.join();
    }

    return 0;
}

Threading in Action

Now we will illustrate the power of parallel programming in C++11. We will do something simple yet interesting: parallel merge sort.