Windows API WaitForMultiObjects(...) 最多只能等待MAXIMUM_WAIT_OBJECTS个kernal objects。MAXIMUM_WAIT_OBJECTS被定义为64。那么这就限制了一次最多只能等待64个内核对象。根据网上查阅的资料,这里给出一种解决方案,允许等待任意多的内核对象(系统支持的范围内)。
- #include <Windows.h>
- /*
- * Synchronically waiting for all objects signaled.
- * - handles : An array of object handles to wait.
- * - count : The count of handles.
- * returns : Same as WaitForMultipleObjects.
- */
- DWORD SyncWaitForMultipleObjs(HANDLE * handles, size_t count)
- {
- int waitingThreadsCount = count;
- int index = 0;
- DWORD res = 0;
- while(waitingThreadsCount >= MAXIMUM_WAIT_OBJECTS)
- {
- res = WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, &handles[index], TRUE, INFINITE);
- if(res == WAIT_TIMEOUT || res == WAIT_FAILED)
- {
- puts("1. Wait Failed.");
- return res;
- }
- waitingThreadsCount -= MAXIMUM_WAIT_OBJECTS;
- index += MAXIMUM_WAIT_OBJECTS;
- }
- if(waitingThreadsCount > 0)
- {
- res = WaitForMultipleObjects(waitingThreadsCount, &handles[index], TRUE, INFINITE);
- if(res == WAIT_TIMEOUT || res == WAIT_FAILED)
- {
- puts("2. Wait Failed.");
- }
- }
- return res;
- }