| Start |
|---|
| std::unique_lock, std::shared_lock, std::lock_guard Used for the RAII style acquiring of try locks, timed try locks and recursive locks.
|
| Strategies for lock classes: std::try_to_lock, std::adopt_lock, std::defer_lock When creating a std::unique_lock, there are three different locking strategies to choose from:
With the following code, whatever happens in the function, the locks are acquired and released in appropriate order:
Something to keep in mind is that std::adopt_lock is not a substitute for recursive mutex usage. When the lock goes out of scope the mutex is released. |
| std::mutex std::mutex is a simple, non-recursive synchronization structure that is used to protect data which is accessed by multiple threads. |
| std::scoped_lock (C++ 17)
|
| Mutex Types C++1x offers a selection of mutex classes:
|
| std::lock
RemarksIt is better to use std::shared_mutex than std::shared_timed_mutex.The performance difference is more than double. If you want to use RWLock, you will find that there are two options. But the implementation is totally different. The code below is MSVC14.1 implementation of std::shared_mutex.You can see that std::shared_mutex is implemented in Windows Slim Reader/Write Locks(https://msdn.microsoft.com/ko-kr/library/windows/desktop/aa904937(v=vs.85).aspx) (opens new window)) Now Let's look at the implementation of std::shared_timed_mutex. The code below is MSVC14.1 implementation of std::shared_timed_mutex.You can see that std::shared_timed_mutex is implemented in std::condition_value. This is a huge difference. So Let's check the performance of two of them. This is the result of read/write test for 1000 millisecond. std::shared_mutex processed read/write over 2 times more than std::shared_timed_mutex.In this example, the read / write ratio is the same, but the read rate is more frequent than the write rate in real. the code below is the code in this example. |
Read Article
https://develop.horje.com/learn/1434/reference