You could use a sleep:
#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();
}
This has the minimal benefit of being portable.
See it live on Coliru
Please be aware of the deadline_timer
class in Boost Asio too.
And it looks like you're trying to await a condition in your worker thread. If so, you can also await a condition_variable
with a deadline (cv.wait_until
or with a timeout: cv.wait_for
).