We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
C++ Memory Model: from C++11 to C++23 - Alex Dathskovsky - CppCon 2023
C++ memory model explained: from C++11 to C++23. Learn about compiler reordering, atomic operations, memory barriers, and synchronization techniques to write correct and efficient multithreaded code.
- C++ memory model is important for multithreaded programming
- We can store data in registers as well as memory
- Compiler can reorder instructions, but not across locks or atomic operations *_compiler Additionally, compiler can do copy elision, which can be beneficial
-
C++20 introduces
std::atomicfor shared pointers -
std::atomic_thread_fenceandstd::memory_ordercan be used to control memory model -
volatilekeyword is not a magic solution, but can be used to prevent compiler optimizations - Memory barriers can be used to ensure sequential consistency
- Latches and barriers are different, but both are used for synchronization
-
stop_sourceandstop_tokencan be used together to control thread execution - C++ has many tools for synchronization, but also can lead to complex code
- Understanding the memory model is important for avoiding data races and ensuring correct program behavior
- Memory order relaxed means that some operations can be reordered, but still ensure the correct result
- Atomic operations are fundamental for multithreaded programming
-
Simple thread synchronization can be achieved using
std::atomic_thread_fence - Compiler optimizations can be used to improve performance, but must be carefully managed
- Multithreaded programming requires careful attention to memory model and synchronization
-
std::shared_ptrcan be used withstd::atomicfor shared ownership - Understanding branch prediction is important for optimizing code
-
C++ standard library provides many useful tools for multithreaded programming, such as
std::thread,std::mutex,std::atomicand more.