C++多线程(五)

来源:互联网 发布:天空左岸java 编辑:程序博客网 时间:2024/05/22 00:55

 


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

1)函数列举

Wait functionDescriptionMsgWaitForMultipleObjectsWaits 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 functionDescriptionCancelWaitableTimerSets 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 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;
}