Multithreading 用另一个超时线程杀死Boost线程
我想在经过一定时间后结束一个线程Multithreading 用另一个超时线程杀死Boost线程,multithreading,boost,boost-thread,Multithreading,Boost,Boost Thread,我想在经过一定时间后结束一个线程WorkerThread。 我正在考虑使用第二个线程TimeoutThread,它在15秒后更改一个标志,使另一个线程停止。 boost中有没有更优雅的方法来实现这一点 #include <boost/thread.hpp> struct MyClass { boost::thread timeoutThread; boost::thread workerThread; bool btimeout = true; v
WorkerThread
。
我正在考虑使用第二个线程TimeoutThread
,它在15秒后更改一个标志,使另一个线程停止。
boost中有没有更优雅的方法来实现这一点
#include <boost/thread.hpp>
struct MyClass
{
boost::thread timeoutThread;
boost::thread workerThread;
bool btimeout = true;
void run()
{
timeoutThread = boost::thread(boost::bind(&MyClass::TimeoutThread, this));
workerThread = boost::thread(boost::bind(&MyClass::WorkerThread, this));
workerThread.join();
TimeoutThread.join();
}
void WorkerThread() {
while(boost::this_thread::interruption_requested() == false && btimeout)
{
printf(".");
}
}
void TimeoutThread()
{
boost::this_thread::disable_interruption oDisableInterruption;
DWORD nStartTime = GetTickCount();
while(boost::this_thread::interruption_requested() == false)
{
if(GetTickCount() - nStartTime > 15)
{
m_bTimeout = false;
break;
}
}
}
};
int main()
{
MyClass x;
x.run();
}
#包括
结构MyClass
{
boost::线程超时线程;
boost::threadworkerthread;
bool btimeout=true;
无效运行()
{
timeoutThread=boost::thread(boost::bind(&MyClass::timeoutThread,this));
workerThread=boost::thread(boost::bind(&MyClass::workerThread,this));
workerThread.join();
TimeoutThread.join();
}
void WorkerThread(){
while(boost::this_thread::interruption_requested()==false&&b超时)
{
printf(“.”);
}
}
void TimeoutThread()
{
boost::this_thread::disable_interruption oDisableInterruption;
DWORD nStartTime=GetTickCount();
while(boost::this_thread::interruption_requested()==false)
{
如果(GetTickCount()-nStartTime>15)
{
m_bTimeout=false;
打破
}
}
}
};
int main()
{
MyClass x;
x、 run();
}
您可以使用睡眠:
#include <boost/thread.hpp>
struct MyClass
{
boost::thread timeoutThread;
boost::thread workerThread;
void TimeoutThread() {
boost::this_thread::sleep_for(boost::chrono::milliseconds(15));
workerThread.interrupt();
}
void WorkerThread() {
while(!boost::this_thread::interruption_requested())
{
//Do stuff
}
}
void run()
{
timeoutThread = boost::thread(boost::bind(&MyClass::TimeoutThread, this));
workerThread = boost::thread(boost::bind(&MyClass::WorkerThread, this));
workerThread.join();
timeoutThread.join();
}
};
int main()
{
MyClass x;
x.run();
}
#包括
结构MyClass
{
boost::线程超时线程;
boost::threadworkerthread;
void TimeoutThread(){
boost::this_线程::sleep_for(boost::chrono::毫秒(15));
workerThread.interrupt();
}
void WorkerThread(){
而(!boost::this_thread::interruption_requested())
{
//做事
}
}
无效运行()
{
timeoutThread=boost::thread(boost::bind(&MyClass::timeoutThread,this));
workerThread=boost::thread(boost::bind(&MyClass::workerThread,this));
workerThread.join();
timeoutThread.join();
}
};
int main()
{
MyClass x;
x、 run();
}
这对便携性的好处微乎其微
查看它
请注意Boost Asio中的deadline\u timer
类
看起来您正在尝试等待工作线程中的条件。如果是这样,您还可以等待一个带有截止日期(
cv.wait\u,直到
或超时:cv.wait\u,等待
)的条件变量。只需检查工作线程中的时间,就不需要单独的超时线程:
void WorkerThread()
{
DWORD nStartTime=GetTickCount();
while(boost::this_thread::interruption_requested()==false&&GetTickCount()-nStartTime<15000)
{
printf(“.”);
}
}
顺便说一句,请注意15000
,因为GetTickCount()单位是毫秒为什么要“破坏”代码,使其无法编译?它几乎是自包含的,但是你删除了类,错误地键入了MyClass bTimeout
,绑定了不存在的无关指针等。在这种情况下,最好保留该类,因为我只是为了编译而不得不重新创建它……而且,你刚才似乎删除了超时线程上的join()
,请注意,这现在是无效的;在应用程序终止之前,必须连接或分离所有线程。LOL在编辑处:/It仍然显示MyClass btimeout代码>并且您仍然在主目录中绑定此
。。。不要介意。现在请看我对SSCCE的回答(以及评论),谢谢!这可能会起作用,但我使用的是boost 1.49.0,我想这似乎只在主干版本中起作用。我使用的是1_55,但AFAIR在更早的版本中就已经出现了versions@tzippy另外,这里是等价的std c++11:和等价的。这不需要任何“新”或“花哨”。@tzippy你收到我的评论了吗^?为什么我们认为他不需要毫秒?@sehe:“15秒后会改变一个标志”,当然这也可能是个错误:)哈哈。我错过了:)
void WorkerThread()
{
DWORD nStartTime = GetTickCount();
while(boost::this_thread::interruption_requested() == false && GetTickCount() - nStartTime < 15000)
{
printf(".");
}
}