更多名人名博

上市公司专栏

实时股价每分更新

纳斯达克(美元)(市值:亿美元)
综指: 涨跌幅:
公司 股价 涨幅度 市值
盛大
网易
九城
畅游
完美
巨人
新浪
百度
恒生指数(港币)(市值:亿港币)
综指: 涨跌幅:
公司 股价 涨幅度 市值
腾讯
金山
网龙
创业板
综指: 涨跌幅:
公司 股价 涨幅度 市值
宝网
d
您现在的位置:首页> 开发|游戏程序|专业工具| > C++多线程(五)

C++多线程(五)

来源:IIEEG03-09-2011

多线程之等待函数
一 等待函数

1)函数列举

Wait function Description
MsgWaitForMultipleObjects Waits 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.
MsgWaitForMultipleObjectsEx Waits 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.
RegisterWaitForSingleObject Directs a wait thread in the thread pool to wait on the object.
SignalObjectAndWait Atomically signals one object and waits on another object.
UnregisterWait Cancels a registered wait operation.
UnregisterWaitEx Cancels a registered wait operation.
WaitForMultipleObjects Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
WaitForMultipleObjectsEx Waits 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.
WaitForSingleObject Waits until the specified object is in the signaled state or the time-out interval elapses.
WaitForSingleObjectEx Waits 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.
WaitOrTimerCallback An application-defined function that serves as the starting address for a timer callback or a registered wait callback.


Waitable-timer function Description
CancelWaitableTimer Sets the specified waitable timer to the inactive state.
CreateWaitableTimer Creates or opens a waitable timer object.
CreateWaitableTimerEx Creates or opens a waitable timer object and returns a handle to the object.
OpenWaitableTimer Opens an existing named waitable timer object.
SetWaitableTimer Activates the specified waitable timer.
TimerAPCProc Application-defined timer completion routine used with the SetWaitableTimer 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核心编程。