Files
2025-10-25 03:02:53 +03:00

2.6 KiB

[thread.lock.guard]

32 Concurrency support library [thread]

32.6 Mutual exclusion [thread.mutex]

32.6.5 Locks [thread.lock]

32.6.5.2 Class template lock_guard [thread.lock.guard]

🔗

namespace std {templateclass 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};}

1

#

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 bypm does not exist for the entire lifetime of the lock_guard object.

The supplied Mutex type shall meet the Cpp17BasicLockable requirements ([thread.req.lockable.basic]).

🔗

explicit lock_guard(mutex_type& m);

2

#

Effects: Initializes pm with m.

Calls m.lock().

🔗

lock_guard(mutex_type& m, adopt_lock_t);

3

#

Preconditions: The calling thread holds a non-shared lock on m.

4

#

Effects: Initializes pm with m.

5

#

Throws: Nothing.

🔗

~lock_guard();

6

#

Effects: Equivalent to: pm.unlock()