Reputation: 59
Kind of C++ and even more Boost noobie here. I have successfully managed to create two threads, based on this example, one Worker and one Interrupter. The latter sleeps, via this_thread::sleep_for for 50 seconds and interrupts the Worker if he is not done within that time, with proper use of while !this_thread::interruption_requested() and thread::interrupt(). So it's a very nice and easy timeout mechanism. My problem is how to stop Interrupter sleep sooner? I mean if Worker finishes before that 50 seconds, I do a this_thread::yield or this_thread::interruption_requested (both seem to work) but Interrupter is still asleep and have to wait for 50 seconds for him to wake up:(
Is there any way to do this timeout mechanism, but if Worker is done with his work before sleep is over, to notify/wake up Interrupter?
PS: Do I need some sort of synchronization when all are done?
Upvotes: 0
Views: 113
Reputation: 1732
Your interrupter thread should not sleep unconditionally for 50 seconds but wait for conditional variable for 50 seconds, if worker thread finishes earlier it will signal conditional var and interrupter would wake up. You can use either std::conditional_variable or one from boost with the same name
Upvotes: 0