C++ Concurrency Cheatsheet

kuniga.me > Docs > C++ Concurrency Cheatsheet

C++ Concurrency Cheatsheet

Index

API

Thread

Includes

#include <thread>

Creation

Thread from lambda

std::thread my_thread([](){
  std::cout << "thread function\n";
});

Assignment version

my_thread_ = std::thread([](){
  std::cout << "thread function\n";
});

Termination

Block until thread ends

my_thread.join();

Locks

RAII lock

std::mutex m;
{
  std::lock_guard<std::mutex> lock(m);
  // locked region
}

Condition variables

Have thread 2 block until thread 1 signals it.

std::condition_variable cv;

// thread 1
ready = true;
cv.notify_one();

// thread 2
std::unique_lock lk(m);
cv.wait(lk, []{return ready;});

Theory

The “synchronizes-with” relationship

This says that if we have write to an atomic variable and then read it, all the operations that happened before the write will have taken effect once we read from the atomic variable, including to non-atomic variables.

Example:

int n;
std::atomic<int> barrier(0);

// executed by thread 1
void f() {
    n = 10;
    // Write to atomic
    barrier.store(1, std::memory_order_release);
}

// executed by thread 2
void g() {
    int ready = barrier.load(std::memory_order_acquire);
    if (ready == 1) {
        // At this point we're guaranteed to "see"
        // n == 10
    }
}