When you use std::unique_lock
you can specify option in constructor.
http://en.cppreference.com/w/cpp/thread/unique_lock/unique_lock
There is three options std::defer_lock_t
, std::try_lock_t
and std::adopt_lock_t
.
std::defer_lock_t
Does not lock the associated mutex.
std::try_lock_t
Tries to lock the associated mutex without blocking by calling m.try_lock()
. The behavior is undefined if the current thread already owns the mutex except when the mutex is recursive.
std::adopt_lock_t
Assumes the calling thread already owns m.
When no option is specified (i.e. constructor called only with mutex param) - mutex is simply locked with mutex.lock()
function.
When you use std::lock_guard
you can only specify one option (std::adopt_lock_t
), in other case mutex will be locked with lock
function.
In all this cases in destructor of lock_guard
/unique_lock
mutex will be unlocked with call to mutex.unlock()
function, if mutex
is locked.