C++11条件变量:wait(lock),notify
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
notify_one()与notify_all()常用来唤醒阻塞的线程。
notify_one()因为只唤醒等待队列中的第一个线程;不存在锁争用所以能够立即获得锁。其余的线程不会被唤醒需要等待再次调用notify_one()或者notify_all()。
notify_all()会唤醒所有等待队列中阻塞的线程存在锁争用只有一个线程能够获得锁。那其余未获取锁的线程接着会怎么样?会阻塞?还是继续尝试获得锁?
答案是会继续尝试获得锁(类似于轮询)而不会再次阻塞。当持有锁的线程释放锁时这些线程中的一个会获得锁。而其余的会接着尝试获得锁。
// condition_variable example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
std::mutex mtx;
std::condition_variable cv;
bool ready = false;
void task_id(int id) {
std::unique_lock<std::mutex> lck(mtx);
while (!ready) cv.wait(lck);
// ...
std::cout << "thread " << id << '\n';
}
void notify() {
std::unique_lock<std::mutex> lck(mtx);
ready = true;
cv.notify_all(); // 这是重点
}
int main()
{
std::thread threads[10];
// spawn 10 threads:
for (int i = 0; i < 10; ++i)
threads[i] = std::thread(task_id, i);
std::cout << "10 threads ready to race...\n";
notify(); // notify!
for (auto& th : threads) th.join();
return 0;
}
运行结果为
10 threads ready to race...
thread 2
thread 0
thread 9
thread 4
thread 6
thread 8
thread 7
thread 5
thread 3
thread 1
输出表明所有线程被唤醒然后依旧获得了锁。
如果将go()中的cv.notify_all()改为cv.notify_one()运行结果为
10 threads ready to race...
thread 0
输出表明只有有一个线程被唤醒然后该线程释放锁这时锁已经处理非锁定状态但是其余线程依旧处于阻塞状态。
所以线程阻塞在condition_variable时它是等待notify_one()或者notify_all()来唤醒。
线程被唤醒后会通过轮询方式获得锁获得锁前也一直处理运行状态不会被再次阻塞。