版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sai_j/article/details/82914636
- Lock doesn’t have to taken right at the construction, you can pass the flag std::defer_lock during its construction to keep the mutex unlocked during construction.
std::unique_lock<std::mutex>(mutex, std::defer_lock);
-
We can unlock it before the function ends and don’t have to necessarily wait for destructor to release it, which can be handy.
-
You can pass the ownership of the lock from a function, it is movable and not copyable.
-
It can be used with conditional variables since that requires mutex to be locked, condition checked and unlocked while waiting for a condition.
std::unique_lock<std::mutex> lock { _mutex };
_condition.wait(lock);