C++多线程(五)

21 篇文章 0 订阅
16 篇文章 0 订阅
多线程之等待函数
一 等待函数

1)函数列举

Wait functionDescription
MsgWaitForMultipleObjectsWaits until one or all of the specified objects are in the signaled state or the time-out interval elapses. The objects can include input event objects.
MsgWaitForMultipleObjectsExWaits until one or all of the specified objects are in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses. The array of objects can include input event objects.
RegisterWaitForSingleObjectDirects a wait thread in the thread pool to wait on the object.
SignalObjectAndWaitAtomically signals one object and waits on another object.
UnregisterWaitCancels a registered wait operation.
UnregisterWaitExCancels a registered wait operation.
WaitForMultipleObjectsWaits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
WaitForMultipleObjectsExWaits until one or all of the specified objects are in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses.
WaitForSingleObjectWaits until the specified object is in the signaled state or the time-out interval elapses.
WaitForSingleObjectExWaits until the specified object is in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses.
WaitOrTimerCallbackAn application-defined function that serves as the starting address for a timer callback or a registered wait callback.


Waitable-timer functionDescription
CancelWaitableTimerSets the specified waitable timer to the inactive state.
CreateWaitableTimerCreates or opens a waitable timer object.
CreateWaitableTimerExCreates or opens a waitable timer object and returns a handle to the object.
OpenWaitableTimerOpens an existing named waitable timer object.
SetWaitableTimerActivates the specified waitable timer.
TimerAPCProcApplication-defined timer completion routine used with theSetWaitableTimer function.


2)简单说明WaitForSingleObject

DWORD WaitForSingleObject(HANDLE hObject,DWORD dwMilliseconds);

参数hObject:要等待的内核对象的句柄。
参数dwMilliseconds: 设置的等待超时的时间,以毫秒为单位。可以设置为INGINIT。     
                                     顺便说一下,INFINITE已经定义为0xFFFFFFFF(或-1)。当然,传递INFINITE有些危险。如果对象永远不变为已
                                     通知状态,那么调用线程永远不会被唤醒,它将永远处于死锁状态。
返回值:WAIT_OBJECT_0表示要等待的对象已经变为已通知的状态。
                 WAIT_TIMEOUT表示设置的时间超时。
                 WAIT_FAILED表示失败,可能是传入的handle不正确或其他的问题。

DWORD dw = WaitForSingleObject(hProcess, 5000);
switch(dw)
{
   case WAIT_OBJECT_0:
      // The process terminated.
      break;

   case WAIT_TIMEOUT:
      // The process did not terminate within 5000 milliseconds.
      break;

   case WAIT_FAILED:
      // Bad call to function (invalid handle?)
      break;
}

3)简单说明WaitForMultipleObjects

DWORD WaitForMultipleObjects(DWORD dwCount,CONST HANDLE* phObjects,BOOL fWaitAll,DWORD dwMilliseconds);

参数dwCout:需要等待的内核对象的数量。
参数phObjects:需要等待的内核对象的是数组的指针。
参数fWaitAll:表示是否需要等待所有的内核对象。
参数dwMilliseconds:设置等待超时的时间。(同上函数)

返回值:WAIT_FAILED和WAIT_TIMEOUT同上函数。
 如果为fWaitAll参数传递TRUE,同时所有对象均变为已通知状态,那么返回值是WAIT_OBJECT_0。如果为fWaitAll传递FALSE       ,那么一旦任何一个对象变为已通知状态,该函数便返回。在这种情况下,你可能想要知道哪个对象变为已通知状态。返回值是WAIT_OBJECT_0与(WAIT_OBJECT_0+dwCount- 1)之间的一个值。

HANDLE h[3];
h[0] = hProcess1;
h[1] = hProcess2;
h[2] = hProcess3;
DWORD dw = WaitForMultipleObjects(3, h, FALSE, 5000);
switch(dw) 
{
   case WAIT_FAILED:
      // Bad call to function (invalid handle?)
      break;

   case WAIT_TIMEOUT:
      // None of the objects became signaled within 5000 milliseconds.
      break;

   case WAIT_OBJECT_0 + 0:
      // The process identified by h[0] (hProcess1) terminated.
      break;

   case WAIT_OBJECT_0 + 1:
      // The process identified by h[1] (hProcess2) terminated.
      break;

   case WAIT_OBJECT_0 + 2:
      // The process identified by h[2] (hProcess3) terminated.
      break;
}


二 参考msdn和windows核心编程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值