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

The std::mutex class provides a basic mutual exclusion and synchronization facility for threads which can be used to protect shared data. Prior to accessing the data protected by the mutex, the mutex must be locked by calling lock() or try_lock(). Only one thread may hold the lock at a time, so if another thread also tries to lock the mutex, it will fail (try_lock()) or block (lock()) as appropriate. Once a thread is done accessing the shared data it then must call unlock() to release the lock and allow other threads to acquire it.

std::mutex meets the Lockable requirements.

For details on the members, see std::mutex class members.

class mutex
{
public:
    mutex(mutex const&)=delete;
    mutex& operator=(mutex const&)=delete;

    mutex();
    ~mutex();

    void lock();
    void unlock();
    bool try_lock();
};
Header

#include <mutex>

See Also