Concurrency Improvements in C++20: A Deep Dive - Rainer Grimm - NDC TechTown 2023

Modern concurrency improvements in C++20, featuring atomic variables, condition variables, and relaxed memory models, enabling efficient and safe multithreading programming.

Key takeaways
  • The std::atomic is used to guarantee the visibility of an update, and atomic::fetch_or is used to test-and-set.
  • C++11 introduces memory_order_relaxed, which doesn’t require an memory-order.
  • A condition variable notify_all call can only work when no thread is blocked or sleeping, if a thread is notified it should stop waiting, wait is like a sleeping that waits until someone notifies you to wake up, notify wakes it up but doesn’t wait if notification happened already.
  • If using atomic store shared pointer is possible without a need of mutex and lock.
  • With FetchAdd, you can have it and notify the thread you can start.
  • CAS(compare-and-swap) compare_exchange_weak atomically changes an atomic, if failed do not check again. Atomically test and set are similar operations.
  • Spurious wake-ups are a situation in which the thread is awakened spuriously before the condition becomes true, they must be handled as lost wake-up.
  • Atomics in C++20 introduces relaxed, acquire-release, and acyclic operations that can reduce locks.
  • Cooperative Interruption in C++20 can suspend the thread which is requested, a feature that allows interrupt a function, useful for thread creation, to stop.
  • Improved threads for better support cooperative interruptions in C++20.
  • Semaphores allow blocking, as well as signaling or non-blocking operations.