Concurrency

This part of the C++ course runs two threads at once. A thread is started and joined, a mutex protects the counter that two threads would otherwise lose, and a condition variable hands work from a producer to a consumer. An atomic and the memory model explain what the mutex did for free, and a future carries a result or an exception back. A task, a promise, a pool and the usual patterns follow; the part ends with a cooperative stop, a coroutine, a generator and the parallel algorithms of the standard.

105 std::thread start a thread, join it, pass arguments and see the output of two threads Open 106 Mutexes and locks std::mutex, lock_guard, scoped_lock and a counter that stops losing updates Open 107 Condition variables wait, notify_one, a predicate and a producer that hands work to a consumer Open 108 Atomics std::atomic, fetch_add, compare_exchange and the same counter without a mutex Open 109 The memory model happens-before, memory_order_relaxed, acquire and release, and a flag that is published Open 110 std::async and std::future launch a task, get its result, wait_for and an exception that crosses the future Open 111 packaged_task and promise pass a result between threads by hand, set_value, set_exception and a worker queue Open 112 A thread pool a queue of tasks, worker threads, a condition variable and a pool that shuts down Open 113 Concurrency patterns producer and consumer, a reader-writer lock, and a bounded queue with back pressure Open 114 std::jthread and stop tokens automatic joining, stop_token and a loop that stops cooperatively Open 115 Coroutines co_await, co_return, a promise type and a coroutine that is resumed by hand Open 116 Generators co_yield, a lazy sequence, a range-for over a generator and the state it keeps Open 117 Parallel algorithms std::execution::par, what this build does with it, and the same loop split across threads Open

Lessons