std::unique_lock
std::unique_lock<Mutex> is a movable RAII lock that can unlock and relock. Condition variables require it. Use lock_guard when you only need "lock for this scope"; use unique_lock when you need to drop the lock before the scope ends.
Detailed Explanation
lock_guard is a bolt. unique_lock is a handle. It still locks in the constructor (by default) and unlocks in the destructor if it owns the mutex. It also has unlock(), lock(), try_lock(), release(), and it is movable. Those extras exist for two jobs: waiting on a condition_variable, and handing a lock to another function.
condition_variable::wait(unique_lock&) must unlock the mutex while the thread sleeps and relock it before wait returns. lock_guard cannot unlock, so it cannot wait. That is the rule that sends you here.
Constructors:
unique_lock(m)— lock now.unique_lock(m, std::defer_lock)— do not lock; you willlock()or pass the pair tostd::lock.unique_lock(m, std::try_to_lock)—try_locknow.unique_lock(m, std::adopt_lock)— assume already locked.- timed versions on a timed mutex.
owns_lock() tells you whether this handle currently holds the mutex. The destructor unlocks only if it owns. After release(), the handle no longer owns and will not unlock; you have taken the obligation back.
Because it is movable, you can return a unique_lock from a function that locked a member mutex so the caller holds it. That is an advanced API and easy to misuse (the mutex must outlive the lock). Prefer locking in the caller.
Cost: unique_lock stores a pointer to the mutex and a bool. lock_guard is slightly leaner. The difference is noise next to the lock itself. Pick based on API need, not on the extra bool.
Do not mix a unique_lock and a lock_guard on the same mutex in one thread. Do not unlock twice. Do not let a unique_lock outlive the mutex.
Code Examples
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
std::mutex mu;
std::condition_variable cv;
bool ready = false;
void waitForReady() {
std::unique_lock<std::mutex> lock(mu);
cv.wait(lock, [] { return ready; }); // unlocks while waiting
std::cout << "ready\n";
}
int main() {
std::thread t(waitForReady);
{
std::lock_guard<std::mutex> lock(mu);
ready = true;
}
cv.notify_one();
t.join(); // prints: ready
}#include <mutex>
#include <utility>
std::mutex mu;
void critical() {
std::unique_lock<std::mutex> lock(mu, std::defer_lock);
lock.lock();
// ...
lock.unlock(); // early release
// non-critical work
}Common Gotchas
Passing a `lock_guard` to `cv.wait`
does not compile. The wait API wants `unique_lock<mutex>`.
`unlock` then destructor
if you `unlock` and forget, the destructor sees `owns_lock() == false` and does nothing. If you `unlock` twice, undefined behaviour. Check `owns_lock` or structure the code so you do not double-unlock.
Movable means you can forget which object owns the lock
after `lock2 = std::move(lock1)`, only `lock2` unlocks.
`defer_lock` plus forgetting `lock()`
you walk the critical section unlocked. `owns_lock()` is the assert to write.