//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);*/
}
转载请注明原文地址: https://ju.6miu.com/read-673179.html