C++-----------notify_one()与notify_all()

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 print_id(int id) {
	std::unique_lock<std::mutex> lck(mtx);
	while (!ready) cv.wait(lck);
	// ...
	std::cout << "thread " << id << '\n';
}
 
void go() {
	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(print_id, i);
 
	std::cout << "10 threads ready to race...\n";
	go();                       // go!
 
	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()来唤醒,而不是等待锁可以被锁定来唤醒。
线程被唤醒后,会通过轮询方式获得锁,获得锁前也一直处理运行状态,不会被再次阻塞。

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
notify_all 和 notify_one 都是 C++ 中的条件变量(condition variable)的成员函数,用于唤醒等待线程。 下面是一个简单的使用实例: ``` #include <iostream> #include <thread> #include <mutex> #include <condition_variable> std::mutex mtx; std::condition_variable cv; bool ready = false; void worker(int id) { std::unique_lock<std::mutex> lock(mtx); while (!ready) { std::cout << "Worker " << id << " is waiting..." << std::endl; cv.wait(lock); } std::cout << "Worker " << id << " is working..." << std::endl; } void notifier() { std::unique_lock<std::mutex> lock(mtx); ready = true; cv.notify_all(); } int main() { std::thread t1(worker, 1); std::thread t2(worker, 2); std::thread t3(worker, 3); std::this_thread::sleep_for(std::chrono::seconds(1)); std::thread t4(notifier); t1.join(); t2.join(); t3.join(); t4.join(); return 0; } ``` 上面的代码创建了三个 worker 线程和一个 notifier 线程。worker 线程会等待 ready 变量为 true,notifier 线程会将 ready 变量设置为 true,并通过 notify_all 函数唤醒所有等待线程。 运行上面的代码,输出结果如下: ``` Worker 1 is waiting... Worker 2 is waiting... Worker 3 is waiting... Worker 3 is working... Worker 2 is working... Worker 1 is working... ``` 可以看到,三个 worker 线程都等待在条件变量上,直到 notifier 线程唤醒它们,并且它们同时开始工作。如果将 notify_all 改为 notify_one,那么只会唤醒一个等待线程,输出结果会有所不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值