WaitForSingleObject and SetEvent
-
Hi Everyone, I have some confusion with WaitForSingleObject. Is it necessary for a thread to be in wait state to handle an event? for example, Thread1 { SetEvent(event); } Thread2{ while(true) { if(WaitForSingleObject(event, INFINITE)){ SetEvent(event); //event raised again, but handler thread 2 is not in wait now } } } Thanks laksh
-
Hi Everyone, I have some confusion with WaitForSingleObject. Is it necessary for a thread to be in wait state to handle an event? for example, Thread1 { SetEvent(event); } Thread2{ while(true) { if(WaitForSingleObject(event, INFINITE)){ SetEvent(event); //event raised again, but handler thread 2 is not in wait now } } } Thanks laksh
All synchronization objects works the same way regarding this, e.g. locking a mutex, releasing a semaphore or setting an event. In your case the event will be set until some thread calls any of the wait functions such as
::WaitForSingleObject()
. The event may be set up to automatically reset when the thread that waits on the event gets released, or it may be set to be "manual reset" in which case you have to call::ResetEvent()
to set the event in a non-signalled state. But, you have to call any of the waiting functions in order to find out whether the event is signalled or not. You may call::WaitForSingelObject()
with a timeout value of zero in which case the function will return withWAIT_TIMEOUT
unless the event was set."It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown