WaitForMultipleObjectsEx
WaitForMultipleObjectsEx waits until one or all of the specified objects are in the signaled state, or the time-out interval elapses. It can also return while the thread is in an APC (asynchronous procedure call) queued state.
C
DWORD WaitForMultipleObjectsEx(
DWORD nCount,
const HANDLE *lpHandles,
BOOL bWaitAll,
DWORD dwMilliseconds,
BOOL bAlertable
);
nCount– Number of object handles in the array pointed to bylpHandles. The maximum value isMAXIMUM_WAIT_OBJECTS(64).lpHandles– Pointer to an array of object handles.bWaitAll– IfTRUE, the function returns when all objects are signaled; otherwise it returns when any object is signaled.dwMilliseconds– Time-out interval, in milliseconds. UseINFINITEto wait indefinitely.bAlertable– IfTRUE, the function returns when the thread enters an alertable state.
If the function succeeds, the return value indicates the event that caused the function to return:
WAIT_OBJECT_0 + i– Thei‑th object was signaled.WAIT_TIMEOUT– The time-out interval elapsed.WAIT_ABANDONED_0 + i– Thei‑th mutex object was abandoned.WAIT_IO_COMPLETION– The function returned because the thread was in an alertable state and an I/O completion routine was executed.
The function can be used with any Windows synchronization object that can be waited on, such as events, mutexes, semaphores, timers, and thread handles.
When bAlertable is TRUE, the thread can execute APCs while waiting. This is useful for I/O completion ports.
#include <windows.h>
#include <stdio.h>
int main(void)
{
HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, L"MyEvent");
if (!hEvent) return 1;
// Simulate signaling after 2 seconds
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)[](LPVOID p)->{
Sleep(2000);
SetEvent((HANDLE)p);
return 0;
}, hEvent, 0, NULL);
DWORD result = WaitForMultipleObjectsEx(1, &hEvent, FALSE, INFINITE, FALSE);
if (result == WAIT_OBJECT_0) {
printf("Event signaled!\\n");
}
CloseHandle(hEvent);
return 0;
}
Comments