concept mutex in category c++

appears as: mutex, mutex
C++ Concurrency in Action, Second Edition

This is an excerpt from Manning's book C++ Concurrency in Action, Second Edition.

The most basic mechanism for protecting shared data provided by the C++ Standard is the mutex, so we’ll look at that first.

  • Class definition
    class mutex
    {
    public:
        mutex(mutex const&)=delete;
        mutex& operator=(mutex const&)=delete;
    
        constexpr mutex() noexcept;
        ~mutex();
    
        void lock();
        void unlock();
        bool try_lock();
    };
  • class mutex
    {
    public:
        mutex(mutex const&)=delete;
        mutex& operator=(mutex const&)=delete;
    
        constexpr mutex() noexcept;
        ~mutex();
    
        void lock();
        void unlock();
        bool try_lock();
    };

    The std::lock_guard class template provides a basic lock ownership wrapper. The type of mutex being locked is specified by template parameter Mutex and must meet the Lockable requirements. The specified mutex is locked in the constructor and unlocked in the destructor. This provides a simple means of locking a mutex for a block of code and ensuring that the mutex is unlocked when the block is left, whether that’s by running off the end, by the use of a control flow statement such as break or return, or by throwing an exception.