【发布时间】:2015-08-14 18:44:50
【问题描述】:
我正在使用下面的简单程序为命令行上指定的参数生成睡眠。
我找不到对应于主线程的boost::thread 对象。使用空的thread_obj,sleep 正在工作,但 boost::thread 对象在我运行程序时不会被中断。
那么有什么特别的原因可以解释为什么我没有收到boost::thread 对象的中断吗?
#include<iostream>
#include<boost/thread/thread.hpp>
#include<boost/date_time/date.hpp>
using namespace boost;
using namespace std;
boost::thread thread_obj;
boost::thread thread_obj1;
void func(void)
{
char x;
cout << "enter y to interrupt" << endl;
cin >> x;
if(x == 'y')
{
cout << "x = 'y'" << endl;
thread_obj.interrupt();
cout << "thread interrupt" << endl;
}
}
int main(int argc,char **argv)
{
thread_obj1 = boost::thread(&func);
boost::system_time const timeout = boost::get_system_time() + boost::posix_time::seconds(atoi(argv[1]));
try
{
boost::this_thread::sleep(timeout);
} catch(boost::thread_interrupted &)
{
cout <<"thread interrupted" << endl;
}
}
【问题讨论】:
-
不知道你在用
thread_obj做什么:它没有附加线程! -
@LightnessRacesinOrbit boost::Thread_obj 只是为了应用睡眠而创建的......有可能这样做......??
-
没有。睡眠将应用在哪个线程上?这没有意义。
-
在 boost::thread 的 thread_obj 对象上......
-
@MaulikPanchal:我想你误解了
boost::thread对象是什么。这就是它没有做你想做的事情的具体原因。或许你可以明确一下你想中断哪个线程,从哪里中断,你希望从哪里休眠。
标签: c++ linux boost-thread