Windows setevent processsing
Posted
by
Waldorf
on Stack Overflow
See other posts from Stack Overflow
or by Waldorf
Published on 2013-10-17T12:09:34Z
Indexed on
2013/10/17
15:56 UTC
Read the original article
Hit count: 89
I wonder how setevent is handled internally within Windows.
I have the following situation
Std::thread thread loop which executes while std::atomic == true Inside the loop is a waitforsingleObject which sleeps infinite in alertable state.
A function stopThread() which does the following: - Clears the atomic bool - Calls Setevent on the event object - Calls thread.join
This often hangs, I get the impression that setevent has still some work to do in the current thread, while join blocks the current thread.
If I add an additional Boolean in the thread which is set after waitforsinlgleObject and I wait for this to be set before calling join() Everything seems to work ok.
Code (error checking omitted here)
Init code/declarations:
HANDLE m_WakeupThreadEvent;
std::atomic<bool> m_ReceiverEnabled;
m_WakeupThreadEvent = CreateEvent(NULL, false, false, "RxThreadWakeupEvent" );
Thread code:
while(m_ReceiverEnabled)
{
DWORD rslt = WaitForSingleObjectEx(m_WakeupThreadEvent, INFINITE, true);
// Here some checking for rslt;
}
function code:
m_ReceiverEnabled = true;
SetEvent( m_WakeupThreadEvent )
m_Thread.join()
Is there some explanation for this behavior ? I could not find any details about the operation of setEvent()
© Stack Overflow or respective owner