C++11 多线程- Mutual exclusion
1 | Mutual exclusion algorithms prevent multiple threads from simultaneously accessing shared resources. This prevents data races and provides support for synchronization between threads. |
头文件 <mutex>
Mutex 系列:
mutex
1、简介: https://en.cppreference.com/w/cpp/thread/mutex
1 |
|
1 |
|
1 | // Example: try_lock |
timed_mutex
recursive_mutex
recursive_timed_mutex
Generic mutex management: 通用互斥 管理器(包装器)
Generic locking algorithms: 通用锁定 算法
Call once:
1 |
|
头文件 <shared_mutex>
涉及 C++14, c++17 暂时不整理
头文件 <condition_variable>
Condition variables
1 | The condition_variable class is a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable. |
1 | std::condition_variable 提供了两种 wait() 函数。 |
1 |
|
1 |
|
生产者-消费者模型:
1 |
|
wait 使用中产生的 a spurious wakeup occurs
, 用以下方式可以 避免
1 | while (!pred()) { |
1 |
|
1 |
|