Tutorial by Examples

When you start a thread, it will execute until it is finished. Often, at some point, you need to (possibly - the thread may already be done) wait for the thread to finish, because you want to use the result for example. int n; std::thread thread{ calculateSomething, std::ref(n) }; //Doing some...
You cannot pass a reference (or const reference) directly to a thread because std::thread will copy/move them. Instead, use std::reference_wrapper: void foo(int& b) { b = 10; } int a = 1; std::thread thread{ foo, std::ref(a) }; //'a' is now really passed as reference thread.join()...
In C++, threads are created using the std::thread class. A thread is a separate flow of execution; it is analogous to having a helper perform one task while you simultaneously perform another. When all the code in the thread is executed, it terminates. When creating a thread, you need to pass somet...
std::this_thread is a namespace which has functions to do interesting things on the current thread from function it is called from. FunctionDescriptionget_idReturns the id of the threadsleep_forSleeps for a specified amount of timesleep_untilSleeps until a specific timeyieldReschedule running threa...
std::async is also able to make threads. Compared to std::thread it is considered less powerful but easier to use when you just want to run a function asynchronously. Asynchronously calling a function #include <future> #include <iostream> unsigned int square(unsigned int i){ r...
When the destructor for std::thread is invoked, a call to either join() or detach() must have been made. If a thread has not been joined or detached, then by default std::terminate will be called. Using RAII, this is generally simple enough to accomplish: class thread_joiner { public: thre...
We can create empty thread objects and assign work to them later. If we assign a thread object to another active, joinable thread, std::terminate will automatically be called before the thread is replaced. #include <thread> void foo() { std::this_thread::sleep_for(std::chrono::seco...
Thread synchronization can be accomplished using mutexes, among other synchronization primitives. There are several mutex types provided by the standard library, but the simplest is std::mutex. To lock a mutex, you construct a lock on it. The simplest lock type is std::lock_guard: std::mutex m; vo...
A condition variable is a primitive used in conjunction with a mutex to orchestrate communication between threads. While it is neither the exclusive or most efficient way to accomplish this, it can be among the simplest to those familiar with the pattern. One waits on a std::condition_variable with...
C++11 threading primitives are still relatively low level. They can be used to write a higher level construct, like a thread pool: C++14 struct tasks { // the mutex, condition variable and deque form a single // thread-safe triggered queue of tasks: std::mutex m; std::condition_variab...
Thread-local storage can be created using the thread_local keyword. A variable declared with the thread_local specifier is said to have thread storage duration. Each thread in a program has its own copy of each thread-local variable. A thread-local variable with function (local) scope will be in...

Page 1 of 1