Summary
In this chapter, we learned how to use the lock-based synchronization primitives provided by the C++ Standard Library.
We started with an explanation of race conditions and the need for mutual exclusion. Then, we studied std::mutex
and how to use it to solve race conditions. We also learned about the main problems when synchronizing with locks: deadlock and livelock.
After learning about mutexes, we studied condition variables and implemented a synchronized queue using mutex and condition variables. Finally, we saw the new synchronization primitives introduced in C++20: semaphores, latches, and barriers.
Finally, we studied the mechanisms provided by the C++ Standard Library to run a function just one time.
In this chapter, we learned about the basic building blocks of thread synchronization and the foundation of asynchronous programming with multiple threads. Lock-based thread synchronization is the most used method to synchronize threads.
In the next chapter...