chacham15
chacham15

Reputation: 14251

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.

Upvotes: 6

Views: 2308

Answers (1)

Martin James
Martin James

Reputation: 24857

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

Upvotes: 13

Related Questions