- 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);