WaitForMultipleObjects等待超过MAXIMUM_WAIT_OBJECTS个的内核对象

Windows API WaitForMultiObjects(…) 最多只能等待MAXIMUM_WAIT_OBJECTS个kernal objects。MAXIMUM_WAIT_OBJECTS被定义为64。那么这就限制了一次最多只能等待64个内核对象。根据网上查阅的资料,这里给出一种解决方案,允许等待任意多的内核对象(系统支持的范围内)。

  1. #include <Windows.h>    
  2.     
  3. /*  
  4. * Synchronically waiting for all objects signaled.  
  5. * - handles : An array of object handles to wait.  
  6. * - count   : The count of handles.  
  7. * returns : Same as WaitForMultipleObjects.  
  8. */    
  9. DWORD SyncWaitForMultipleObjs(HANDLE * handles, size_t count)    
  10. {    
  11.     int waitingThreadsCount = count;    
  12.     int index = 0;    
  13.     DWORD res = 0;    
  14.     while(waitingThreadsCount >= MAXIMUM_WAIT_OBJECTS)    
  15.     {    
  16.         res = WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, &handles[index], TRUE, INFINITE);    
  17.         if(res == WAIT_TIMEOUT || res == WAIT_FAILED)    
  18.         {    
  19.             puts(”1. Wait Failed.”);    
  20.             return res;    
  21.         }    
  22.     
  23.         waitingThreadsCount -= MAXIMUM_WAIT_OBJECTS;    
  24.         index += MAXIMUM_WAIT_OBJECTS;    
  25.     }    
  26.     
  27.     if(waitingThreadsCount > 0)    
  28.     {    
  29.         res = WaitForMultipleObjects(waitingThreadsCount, &handles[index], TRUE, INFINITE);    
  30.         if(res == WAIT_TIMEOUT || res == WAIT_FAILED)    
  31.         {    
  32.             puts(”2. Wait Failed.”);    
  33.         }    
  34.     }    
  35.     
  36.     return res;    
  37. }    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值