boost 唤醒机制(windows下)

本文探讨了Linux与Windows环境下线程唤醒机制的区别。Linux下唤醒操作立即执行,若无挂起线程则不等待;而Windows下唤醒操作会等待首个挂起线程并唤醒之。通过示例代码进一步解释了这两种机制的具体行为。
//linux线程的唤醒机制与windows的不同
/*在windows下如果执行了线程唤醒操作,该唤醒操作会等待第一个挂起的线程,当系统中有线程挂起的时候,
就对其进行唤醒。其生存周期一直到其执行完一次唤醒任务结束。
在linux下,如果执行唤醒操作,该唤醒操作会立刻执行,如果系统中没有挂起的线程,
那么该操作就会立刻执行结束,如果有线程在操作执行结束后挂起,则不会被唤醒。唤醒操作的生存周期是,执行操作的瞬间,
有线程挂起就唤醒,没有操作结束。不像windows那样,等待第一个需要的唤醒操作到达。*/

#include "stdafx.h"
#include <stdlib.h>
#include<iostream>
#include<boost/thread/condition.hpp>
#include<boost/thread/locks.hpp>
#include<boost/thread/mutex.hpp>
#include <boost/thread/detail/thread.hpp>
#include <boost/thread/thread_time.hpp>
#include <iostream>
#include <Windows.h>
using namespace boost::posix_time;
using namespace std;
boost::mutex token;
boost::condition_variable num_cond;
int thread_amount = 0;
void ThreadOne()
{
	boost::mutex::scoped_lock tokenlock(token);
	cout << "thread one wait" << endl;
	num_cond.wait(tokenlock);
	cout << "thread one work" << endl;;
}
void ThreadTwo()
{
	boost::mutex::scoped_lock tokenlock(token);
	cout << "thread two wait" << endl;
	num_cond.wait(tokenlock);
	cout << "thread two work" << endl;
}
void Awoke()
{
	boost::mutex::scoped_lock tokenlock(token);
	cout << "awake now" << endl;
	num_cond.notify_one();
}

int main(int argc, char** argv)
{
	/*boost::thread aw(&Awoke);
	Sleep(1);
	boost::thread w(&ThreadOne);
	boost::thread w2(&ThreadTwo);
	w.join();
	aw.join();
	w2.join();
	return (EXIT_SUCCESS);*/

	//当唤醒线程执行后,后到的挂起线程,不会被之前的唤起线程唤醒 二者均不会唤醒

	///

	/*boost::thread w(&ThreadOne);
	Sleep(1);
	boost::thread w2(&ThreadTwo);
	Sleep(1);
	boost::thread aw(&Awoke);
	w.join();
	aw.join();
	w2.join();
	return (EXIT_SUCCESS);
*/
	///只执行一次唤醒操作结束,当有线程挂起后,唤醒时候按照先进现出的原则,将第一个wait的线程唤醒。
	

	/*boost::thread w(&ThreadOne);
	Sleep(1);
	boost::thread w2(&ThreadTwo);
	Sleep(1);
	boost::thread aw(&Awoke);
	w.join();
	w2.join();
	aw.join();
	return (EXIT_SUCCESS);*/


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值