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::atomic
for shared pointers -
std::atomic_thread_fence
andstd::memory_order
can be used to control memory model -
volatile
keyword 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_source
andstop_token
can 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_ptr
can be used withstd::atomic
for 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::atomic
and more.