Tutorial by Examples

Used for the RAII style acquiring of try locks, timed try locks and recursive locks. std::unique_lock allows for exclusive ownership of mutexes. std::shared_lock allows for shared ownership of mutexes. Several threads can hold std::shared_locks on a std::shared_mutex. Available from C++ 14. std:...
When creating a std::unique_lock, there are three different locking strategies to choose from: std::try_to_lock, std::defer_lock and std::adopt_lock std::try_to_lock allows for trying a lock without blocking: { std::atomic_int temp {0}; std::mutex _mutex; std::thread t( [&...
std::mutex is a simple, non-recursive synchronization structure that is used to protect data which is accessed by multiple threads. std::atomic_int temp{0}; std::mutex _mutex; std::thread t( [&](){ while( temp!= -1){ ...
std::scoped_lock provides RAII style semantics for owning one more mutexes, combined with the lock avoidance algorithms used by std::lock. When std::scoped_lock is destroyed, mutexes are released in the reverse order from which they where acquired. { std::scoped_lock lock{_mutex1,_mutex2}; ...
C++1x offers a selection of mutex classes: std::mutex - offers simple locking functionality. std::timed_mutex - offers try_to_lock functionality std::recursive_mutex - allows recursive locking by the same thread. std::shared_mutex, std::shared_timed_mutex - offers shared and unique lock functi...
std::lock uses deadlock avoidance algorithms to lock one or more mutexes. If an exception is thrown during a call to lock multiple objects, std::lock unlocks the successfully locked objects before re-throwing the exception. std::lock(_mutex1, _mutex2);

Page 1 of 1