30 Thread support library [thread]

30.4 Mutual exclusion [thread.mutex]

30.4.2 Locks [thread.lock]

30.4.2.1 Class template lock_guard [thread.lock.guard]

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

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

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

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

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 ([basic.life]). 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 ([thread.req.lockable.basic]).

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: m.lock()

Postcondition: &pm == &m

lock_guard(mutex_type& m, adopt_lock_t);

Requires: The calling thread owns the mutex m.

Postcondition: &pm == &m

Throws: Nothing.

~lock_guard();

Effects: pm.unlock()