What is the difference between std::lock_guard and std::unique_lock, and when is the flexibility of unique_lock necessary?

Last updated 21 June 2026

  • Both are RAII wrappers that lock a mutex on construction and unlock on destruction. std::lock_guard is the minimal, zero-overhead default that can't be unlocked early or moved.

  • std::unique_lock supports manual lock()/unlock(), deferred locking (std::defer_lock), timed locking, and is movable. It has small overhead.

  • Use unique_lock with std::condition_variable::wait (which must unlock and relock), for releasing a lock mid-scope, or for transferring lock ownership.

  • https://utkrusht.ai/blog/cplusplus-interview-questions
  • https://roadmap.sh/questions/cpp
  • https://internshala.com/blog/cpp-coding-interview-questions/
All C++ interview questions