Memory Model
The foundation of multithreading is a well-defined memory model. Having a basic understanding of the memory helps a lot to get a deeper insight into the multithreading challenges.
Don’t use volatile for synchronisation
In C++ volatile
has no multithreading semantic in contrast to C# or Java. In C# or Java, volatile
declares an atomic such as std::atomic
declares an atomic in C++ and is typically used for objects which can change independently of the regular program flow. Due to this characteristic, no optimised storing in caches takes place.
Don’t program Lock Free
This advice sounds ridiculous after writing a book about concurrency and having an entire chapter dedicated to the memory model. The reason for this advice is quite simple. Lock-free programming is very error-prone and requires an expert level in this unique domain. In particular, if you want to implement a lock-free data structure, be aware of the ABA problem.