...

/

Types of Locks: std::unique_lock

Types of Locks: std::unique_lock

This lesson gives an overview of std::unique_lock which is a type of lock used in C++.

Features

In addition to what’s offered by a std::lock_guard, a std::unique_lock enables you to

  • create it without an associated mutex.
  • create it without locking the associated mutex.
  • explicitly and repeatedly set or release the lock of the associated mutex.
  • move the mutex.
  • try to lock the mutex.
  • delay the lock on the associated mutex.

Methods

The following table shows the methods of a std::unique_lock lk.

Method Description
lk.lock() Locks the associated mutex.
std::lock(lk1, lk2, ... ) Locks atomically the arbitrary number of associated mutexes.
lk.try_lock() and lk.try_lock_for(relTime) and
...