How is std :: unique_lock different from std :: lock_guard? In what situations is it better to use one class and in what other?
Answer 1, authority 100%
lock_guard
and unique_lock
are pretty much the same. The difference is that you can lock and unlock std :: unique_lock
. std :: lock_guard
will lock only once on build and unlock on destruction.
- lock_guard : when an object is created, it tries to acquire a mutex
(by callinglock ()
) and when the object is destroyed it is automatically
frees the mutex (by callingunlock ()
). - unique_lock : also supports deferred locking, temporary locking, recursive locking, and using conditional variables.