I have thread which sleeps for a long time, then wakes up to do something, then sleep again, like this:
while(some_condition)
{
// do something
sleep(1000);
}
How could I make this thread exit gracefully and QUICKLY?
I tried to use pthread_cancel(), but sleeping threads could not be canceled.
I also tried changing the condition of the while loop, but it will still take long to exit.
And I don’t want to use pthread_kill(), since it may kill the thread when it’s working.
So, are there any good ideas?
As an alternative to
sleep, you could use pthread_cond_timedwait with a 1000 ms timeout. Then when you want to exit, signal the condition variable.This is similar to how you might do this in C#/Java using wait and notify.