Documentation Home >> Headers >> <mutex> Header >> std::unique_lock class template

The std::unique_lock class template provides a more general lock ownership wrapper than std::lock_guard. The type of mutex being locked is specified by the template parameter Mutex, which must meet the Lockable requirements. In general, the specified mutex is locked in the constructor and unlocked in the destructor, though additional constructors and member functions are provided to allow other possibilities. This provides a means of locking a mutex for a block of code and ensuring that the mutex is unlocked when the block is left, whether that is by running off the end, by the use of a control flow statement such as break or return, or by throwing an exception. The wait functions of std::condition_variable require an instance of std::unique_lock<std::mutex>, and all instantiations of std::unique_lock are suitable for use with the Lockable parameter for the std::condition_variable_any wait functions.

If the supplied Mutex type meets the Lockable requirements, then std::unique_lock<Mutex> also meets the Lockable requirements. If, in addition, the supplied Mutex type meets the TimedLockable requirements, then std::unique_lock<Mutex> also meets the TimedLockable requirements.

Instances of std::unique_lock are MoveConstructible and MoveAssignable, but not CopyConstructible or CopyAssignable.

template <class Mutex>
class unique_lock
    typedef Mutex mutex_type;

    explicit unique_lock(mutex_type& m);
    unique_lock(mutex_type& m, adopt_lock_t);
    unique_lock(mutex_type& m, defer_lock_t);
    unique_lock(mutex_type& m, try_to_lock_t);

    template<typename Clock,typename Duration>
        mutex_type& m,
        std::chrono::time_point<Clock,Duration> const&

    template<typename Rep,typename Period>
        mutex_type& m,
        std::chrono::duration<Rep,Period> const&


    unique_lock(unique_lock const& ) = delete;
    unique_lock& operator=(
        unique_lock const& ) = delete;

    unique_lock(unique_lock&& );
    unique_lock& operator=(unique_lock&& );

    void swap(unique_lock&& other);

    void lock();
    bool try_lock();
    template<typename Rep, typename Period>
    bool try_lock_for(
        std::chrono::duration<Rep,Period> const&
    template<typename Clock, typename Duration>
    bool try_lock_until(
        std::chrono::time_point<Clock,Duration> const&
    void unlock();

    explicit operator bool() const;
    bool owns_lock() const;
    Mutex* mutex() const;
    Mutex* release();

#include <mutex>

See Also