C++ Multithreading Support

C++11 Multithreading overview

The C++11 standard provides multithreading support, including the following features: thread creation and management, mutexes, atomic objects, thread local storage and condition variables.

Thread creation and management is provided by the std::thread class which is defined in <thread>. The class works with regular functions, lambdas (functions written directly in your source code) or functors (classes with overloaded () operator). Any number of parameters can be passed to the thread function. To work with the thread you should create an instance of std::thread class and pass it the working function. Let’s see an example:

In this example a reference to the trivial function is being passed to the constructor of the thread class. After thread creation we call join() to ensure that it has finished executing before program exit. Technically you should check whether the thread is joinable by calling its joinable() method first. If you don’t want to wait for thread execution finish, you could call the detach() member function to separate thread execution from the thread object. After calling detach() this thread object is no longer joinable. Any number of arguments can be passed to a thread:

If there is need to pass parameters by reference, they should be wrapped into std::ref or std::cref.
Each thread has its own id. std::thread class has get_id() member function, which returns unique id for the thread. To get the id of the current thread std::this_thread::get_id() can be used. Thread id’s are stored in lightweight std::thread::id class.

Useful features

There are several useful functions in this_thread namespace. yield() function is used to notify the thread scheduler that it should select another thread to run. Using yield() is rarely necessary. sleep_until() expects an instance of std::chrono::time_point class and stops execution till system clock reaches this point. sleep_for() expects an instance of std::chrono::duration class and blocks thread execution for that certain time.

Please try to run the listing above several times. As you can see the results are different every time. That’s because each thread is printing information using stdout which is a shared resource. Because output is not synchronized between working threads, its ownership could be easily “stolen” by another threads. The result of such non-synchronized access is undefined behavior.

Mutex is used to ensure that only one thread is working with the shared resource at the same time. So let’s talk a bit about how they are defined in C++11 standard. Class std::mutex is used for locking and unlocking shared resources. Besides, there are three different types of mutexes:

std::timed_mutex, which differs from previous one only by having a possibility to lock some resource for a certain timeout.

std::recursive_mutex. This kind of mutex allows a thread to recursively lock a resource, by making additional calls to lock or try_lock. The period of ownership ends when thread makes a matching number of calls to unlock. Attempting to recursively lock a regular mutex will cause a deadlock.

std::recursive_timed_mutex. Recursive mutex with timeout ability. Let’s change previous example and make access to the shared resource managed as we want it to be.

The main function is the same as in previous listing so we’ve just skipped it here.

std::lock_guard is a RAII-style wrapper for more convenient work with std::mutex, it locks on creation and releases the lock after destruction. Because of potential deadlock problems, it is strongly recommended to avoid locking several mutexes at one time. Unfortunately, sometimes more than one resource should be locked (e.g. you are working with two distinct data items and each of them is protected by its own mutex). In that case std::unique_lock can be helpful. It has a similar functionality to lock_guard but besides that it has a mechanism for avoiding deadlocks in case each resource should be locked by different mutex. An additional argument is passed to its constructor which defines locking strategy. By passing std::defer_lock you can tell std::unique_lock not to acquire ownership of the mutex immediately, and then safely lock both resources by std::lock:

Please keep in mind that the behavior of a program is undefined in case a mutex is destroyed while still owned by some thread. A thread owns the mutex since the time the mutex is successfully locked till the call of unlock method. So you have to be sure that mutex is not locking anything before destroying it.

Condition variables are another synchronization primitive which is provided by the C++11 standard. It is always associated with mutexes and allows threads to communicate with each other. Also it allows multiple threads to wait for some certain condition to become true or for a specified timeout to expire. To work with a condition variable thread must acquire a lock first and pass the lock to subsequent condition variable operations. This lock will be implicitly released when the thread starts to wait on the condition variable and when the thread is being awakened because of conditional variable wakeup. std::condition_variable can block threads until notification is received or timeout is expired and sometimes when spurious wakeup occurs.

Besides std::condition_variable, there is an std::condition_variable_any. The thing is std::condition_variable can work only with std::unique_lock<std::mutex>, where std::condition_variable_any can work with any (possibly used-defined) type which meets the BasicLockable requirements. Besides that, the functionality of both synchronization primitives is almost identical.

C++ threads vs. Boost threads

One of the most common used open-source libraries for C++ is Boost. This library also provides multithreading support, so let’s compare C++11 threads with boost threads. C++11 threads are supported at core language level while boost threads are basically wrappers around platform-dependent thread implementations.

First thing which you may notice by now is that a C++ thread will not have its detach() method implicitly called when it is being destructed. You have to manually call join() or detach() before destroying a thread. If you don’t, the program will call std::terminate() and it will abort your application. In Boost there is no such problem because detach() is called automatically on thread destruction. Boost threads support cancellation, but C++ standard threads don’t.

std::thread allows move-only types, such as std::unique_ptr to be passed as arguments. Because of the use of boost::bind internally, Boost requires copyable arguments. To clear things up a little bit let’s see an example:

std::unique_ptr cannot be copied so this example won’t work. It works when using std::thread, but it won’t be working with boost::thread due to its semantics requiring copyable arguments. Migration from Boost threads to C++11 should not cause a lot of trouble. Of course, some of the Boost features are not supported in C++ standard yet. For example, there is no analogue of boost::shared_mutex, which allows multiple-reader and single-writer locking. It is planned to include such kind of mutex in future C++ standard (from C++14), but currently it is not implemented. Also using threads from the language standard gives few advantages comparing to usage of Boost threads. No matter how convenient Boost is it’s still an additional library, which means you need to rely on Boost libraries during compilation and linkage. Sometimes these libraries need to be updated. When using C++ threads, which are already included into the standard you don’t need to worry about such things.

How it works

We’ve seen the main features of the C++ threading model. Let’s try to use these features while solving some practical task. Producer-consumer is a well-known multithreading problem. It describes two threads working with a single queue. One of them produces something and puts it into queue and another is consuming and removes items from the queue. The main problem is to ensure that the producer won’t try to add into the queue when it’s full and consumer won’t try to consume from the queue when it’s empty.

We declared a boolean flag to indicate ready status of queue, the global message queue to put data in it, and mutex for locking shared data while working with it. Also we a declare condition variable for communication between threads. To prevent hardcoding we declare constants for queue size and sleep time. Producer function will look like this:

Here we have a an infinite loop which pushes data to queue. On each iteration it has small delay and total number of queue elements is compared with queue size. If it less than the maximum queue size data is being pushed to the queue, otherwise the status flag is given the appropriate status and the condition variable is used to send notification to the consumer thread. Lock is acquired when working with shared resources. Now in the consumer function we have one more infinite loop. It uses the status flag which prevents it from spurious wakeups and waits for notification form the producer thread. After a notification has been received the consumer pops data from the queue and sets the status flag to false:

Teodor Moroz is a software engineer, developing solutions for backup and restoration using pure C. Sometimes Teodor also makes small mobile apps for Android phones and he is planning to start creating Flash movies. Traveling, playing guitar, literature and philosophy are also among his hobbies. Teodor finds blogging a great way to enhance knowledge and fill his knowledge gaps.

1999-2014 (C) ELEKS, All Rights Reserved. ELEKS is a global organization that provides end-to-end product development since 1991. Our innovative solutions including Data Science, Mobility & Wearables as well as Digital solutions have significantly contributed to the success of our clients, including Fortune 500 companies.