std::mutex (C++11)
In a Nutshell
The most basic mutex, allowing only one thread to hold it at a time, used to protect shared data across multiple threads.
Header
#include <mutex>
Core API Quick Reference
| Operation | Signature | Description |
|---|---|---|
| Construct | mutex() | Constructs the mutex |
| Destruct | ~mutex() | Destroys the mutex |
| Lock | void lock() | Locks the mutex, blocks if unavailable |
| Try lock | bool try_lock() | Tries to lock the mutex, returns false immediately if unavailable |
| Unlock | void unlock() | Unlocks the mutex |
| Native handle | native_handle_type native_handle() | Returns the underlying implementation-defined native handle |
Minimal Example
cpp
#include <iostream>
#include <mutex>
#include <thread>
int counter = 0;
std::mutex m;
void increment() {
std::lock_guard<std::mutex> lock(m);
++counter;
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << counter << '\n'; // 输出: 2
}Embedded Applicability: High
- Typically a zero-overhead abstraction, incurring only atomic operation overhead when uncontended
- Non-copyable and non-movable, with a clear and controllable memory layout
- We recommend using it with
lock_guardto avoid deadlocks on exception paths - Note: in an RTOS environment, we must ensure that the underlying pthread or OS primitives are available
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 4.4 | 3.3 | 2010 |
See Also
Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0