adoptlock
Adopt_lock is a C++ standard library tag used with locking constructors to indicate that a mutex is already locked by the current thread and that a lock object should adopt that existing ownership rather than performing a new lock operation. It is defined in the mutex facilities as std::adopt_lock_t and is exposed as the tag object std::adopt_lock.
Usage typically involves std::unique_lock, not std::lock_guard. A common pattern is to acquire the mutex through some
std::unique_lock<std::mutex> ul(m, std::adopt_lock);
In this case, the unique_lock takes ownership of the already-held lock and will release the mutex when
Adopt_lock is often employed to transfer lock ownership between guards or to interface with functions that
- Adopt_lock signals that the mutex is already locked by the calling thread and should be adopted
- It does not perform a locking operation.
- The resulting lock object will unlock the mutex upon destruction.
- It is specific to std::unique_lock and to the standard mutex facilities in <mutex>.
- Correct use requires that the current thread owns the mutex at the time of adoption.