std::lock_guard

From Cppreference

Jump to: navigation, search
Defined in header <mutex>

template<typename Mutex>
class lock_guard;
(C++11 feature)

The class lock_guard implements a strictly scope-based mutex ownership wrapper. The class is non-copyable. The supplied Mutex type shall implement the Lockable concept.

[edit] Member types

Member type Definition
mutex_type Mutex

[edit] Member functions

(constructor)
constructs new lock_guard object and locks the given mutex
(public member function)
(destructor)
destructs the lock_guard object, unlocks the underlying mutex
(public member function)

[edit] Example

std::mutex m;
 
void do_something()
{
    std::lock_guard<std::mutex> lock(m);
    // the remaining function is thread-safe now
}