I want to end a thread WorkerThread
after a certain amount of time has elapsed.
I was thinking to use a second thread TimeoutThread
for this, that changes a flag after 15 seconds so the other thread stops.
Is there a more elegant way in boost to do this?
#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();
}