33 Thread support library [thread]

33.4 Mutual exclusion [thread.mutex]

33.4.4 Locks [thread.lock]

33.4.4.1 Class template lock_­guard [thread.lock.guard]

namespace std {
  template <class Mutex>
  class lock_guard {
  public:
    using mutex_type = Mutex;

    explicit lock_guard(mutex_type& m);
    lock_guard(mutex_type& m, adopt_lock_t);
    ~lock_guard();

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

  private:
    mutex_type& pm; // exposition only
  };

  template<class Mutex> lock_guard(lock_guard<Mutex>) -> lock_guard<Mutex>;
}

An object of type lock_­guard controls the ownership of a lockable object within a scope. A lock_­guard object maintains ownership of a lockable object throughout the lock_­guard object's lifetime. The behavior of a program is undefined if the lockable object referenced by pm does not exist for the entire lifetime of the lock_­guard object. The supplied Mutex type shall meet the BasicLockable requirements.

explicit lock_guard(mutex_type& m);

Requires: If mutex_­type is not a recursive mutex, the calling thread does not own the mutex m.

Effects: As if by m.lock().

Postconditions: &pm == &m

lock_guard(mutex_type& m, adopt_lock_t);

Requires: The calling thread owns the mutex m.

Postconditions: &pm == &m

Throws: Nothing.

~lock_guard();

Effects: As if by pm.unlock().