boost的线程库不能强行终止,所以通过time_wait可以让其自然的结束
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost;
boost::mutex test_mutex;
boost::condition_variable test_condition;
void test()
{
for (;;)
{
boost::mutex::scoped_lock lock(test_mutex);
if (test_condition.timed_wait(lock, get_system_time() + posix_time::seconds(3)))
{
cout << "成功接收到通知" << endl; //这里加个break就结束了
}
else
{
cout << "没有等待到通知" << endl;
}
}
}
int main() {
boost::thread test_thread(test);
for (;;)
{
::system("PAUSE");
cout << "开始发送通知" << endl;
test_condition.notify_one();
}
}
【转自:http://www.cppblog.com/thisisbin/archive/2009/02/19/74311.html】