Skip to content
Search

WindowsApi:CreateEvent

lastmod 2021-11-19created 2016-03-282 edits

One of the synchronization objects. After creating an event object, you can put the event into the Signal or Nonsignal state.

How to use CreateEvent

The prototype is as follows.

HANDLE CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes, // 요건 모바일환경에는 항상 NULL로 해줘야 함다
BOOL bManualReset, // 수동(TRUE), 자동(FALSE)
BOOL bInitialState, // 생성시 이벤트가 적용된 상태이냐 아니냐
LPTSTR lpName); // 이벤트네임
  • If the second parameter (bManualReset) is set to manual (FALSE) and the event is raised with the SetEvent() API, then in manual mode you must call the ResetEvent() API after the event fires before the event can fire again.
  • The third parameter determines whether the event is signaled from the moment it is created. If you create it with TRUE, it starts with the event already raised; otherwise it waits for the event.
  • The fourth parameter assigns an event name, which I find very useful for inter-process communication. If you just give the same event name in completely unrelated processes and raise it (SetEvent()), the other process responds.

Use it as follows.

HANDLE hEvent;
hEvent = CreateEvent(NULL, FALSE, FALSE, L"STUPID");

SetEvent(hEvent);         //hEvent 발생
ResetEvent(hEvent);       //hEvent 재생(수동일때만...)

To wait for an event, use the WaitForSingleObject() and WaitForMultiObject() APIs. As with all handles, use CloseHandle() when finished.

Comparison with Linux

Linux has no dedicated mechanism for this, so it is implemented with a semaphore. You just need to set the initial value to 0 at initialization.

Windows

Linux

CreateEvent()
OpenEvent()

sem_init()

SetEvent()

sem_post()

WaitForSingleObject()

sem_wait()
sem_trywait()

CloseHandle()

sem_destroy()

See also

author Zer0 sitemap