多线程之等待函数
一 等待函数
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. |
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核心编程。