Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create interruptible sleep in Windows with C?

I want the current thread to sleep for a given time. However, another thread should be able to interrupt it and wake it up early. In unix this is fairly simple using sleep + pthread_kill. In windows there is SleepEx and SleepConditionVariableCS. SleepEx doesnt seem to actually cause the thread to sleep since it is still processing events, so would sleeping on a condition variable be a better solution? Furthermore, it is somewhat unclear to me how to wake a thread sleeping with SleepEx. What is correct solution to this problem, SleepEx or SleepConditionVariableCS? (Also, could you point out how to wake a thread sleeping with SleepEx? The MSDN documentation is very confusing.

like image 267
chacham15 Avatar asked Jan 26 '13 16:01

chacham15


1 Answers

Create a manual reset event and wait on it with WaitForSingleObject - has a timeout parameter. See MSDN for details.

like image 151
Martin James Avatar answered Oct 13 '22 01:10

Martin James