-
Notifications
You must be signed in to change notification settings - Fork 24
/
event-experiment.c
32 lines (25 loc) · 1.24 KB
/
event-experiment.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <Windows.h>
// The typical usage pattern for an event is to signal when something completes and unsignal while something is incomplete
// For example, the LdrpInitCompleteEvent loader event signals when loader initialization is complete
// https://learn.microsoft.com/en-us/windows/win32/sync/event-objects
// https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-resetevent#remarks
int main() {
// Create an unsignalled auto-reset event
HANDLE myEvent = CreateEvent(NULL, FALSE, FALSE, L"MyEvent");
if (myEvent == 0)
return 1;
// Set event
SetEvent(myEvent);
__debugbreak();
// Event: Set -> Waiting
// Execution proceeds
WaitForSingleObject(myEvent, INFINITE);
// Event is waiting, so execution waits
// WinDbg command: !handle MyEvent ff
// We expectedly hang here
WaitForSingleObject(myEvent, INFINITE);
// When an auto-reset event is set, execution only proceeds for a SINGLE waiting thread before the event waits.
// This behavior means an auto-reset event performs mutual exclusion between threads, similar to a critical section.
//
// Unlike a critical section, though, an auto-reset event doesn't support recursive acquisition on the same thread.
}