本文主要是介绍MFC WaitForMultipleObjects 函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
WaitForMultipleObjects 函数
- Syntax
- Parameters
- Return Value
- Remarks
Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
【等待,直到一个或所有指定的对象处于信号状态或超时间隔结束。】
To enter an alertable wait state, use the WaitForMultipleObjectsEx function.
【要进入可报警的等待状态,请使用WaitForMultipleObjectsEx函数。】
Syntax
DWORD WINAPI WaitForMultipleObjects(__in DWORD nCount,__in const HANDLE* lpHandles,__in BOOL bWaitAll,__in DWORD dwMilliseconds
);\
Parameters
nCount | The number of object handles in the array pointed to by lpHandles. The maximum number of object handles is MAXIMUM_WAIT_OBJECTS. lpHandles指向的数组中的对象句柄数。对象句柄的最大数目是MAXIMUM_WAIT_OBJECTS。 |
lpHandles | An array of object handles. For a list of the object types whose handles can be specified, see the following Remarks section. The array can contain handles to objects of different types. It may not contain multiple copies of the same handle. 【对象句柄数组。有关可以指定其句柄的对象类型的列表,请参见下面的备注部分。数组可以包含不同类型对象的句柄。它不能包含同一句柄的多个副本。】 If one of these handles is closed while the wait is still pending, the function’s behavior is undefined. 【如果其中一个句柄在等待仍挂起时关闭,则函数的行为未定义。】 The handles must have the SYNCHRONIZE access right. For more information, see Standard Access Rights. 【句柄必须具有同步访问权限。有关详细信息,请参阅标准访问权限。】 |
bWaitAll | If this parameter is TRUE, the function returns when the state of all objects in the lpHandles array is signaled. If FALSE, the function returns when the state of any one of the objects is set to signaled. In the latter case, the return value indicates the object whose state caused the function to return. 【如果此参数为TRUE,则当lpHandles数组中所有对象的状态都被通知时,函数返回。如果为FALSE,则当任何一个对象的状态设置为signaled时,函数返回。在后一种情况下,返回值指示其状态导致函数返回的对象。】 |
dwMilliseconds | The time-out interval, in milliseconds. The function returns if the interval elapses, even if the conditions specified by the bWaitAll parameter are not met. If dwMilliseconds is zero, the function tests the states of the specified objects and returns immediately. If dwMilliseconds is INFINITE, the function’s time-out interval never elapses. 【超时间隔,以毫秒为单位。如果间隔已过,则函数返回,即使bWaitAll参数指定的条件未满足也是如此。如果dwMilliseconds为零,则函数将测试指定对象的状态并立即返回。如果dwMilliseconds是无限的,则函数的超时时间间隔永远不会过去。】 |
Return Value
If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values. (Note that WAIT_OBJECT_0 is defined as 0 and WAIT_ABANDONED_0 is defined as 0x00000080L.)
【如果函数成功,则返回值指示导致函数返回的事件。它可以是以下值之一。(请注意,WAIT_OBJECT_0定义为0,WAIT_ABANDONED_0定义为0x00000080L。)】
Return code/value | Description |
---|---|
WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount– 1) | If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled. 【如果bWaitAll为TRUE,则返回值指示所有指定对象的状态都被通知。】 If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 indicates the lpHandles array index of the object that satisfied the wait. If more than one object became signaled during the call, this is the array index of the signaled object with the smallest index value of all the signaled objects. 【如果bWaitAll为FALSE,则返回值减去WAIT_OBJECT_0 表示满足等待的对象的lpHandles数组索引。如果在调用期间有多个对象发出信号,则这是所有发出信号的对象中具有最小索引值的发出信号的对象的数组索引。】 |
WAIT_ABANDONED_0 to (WAIT_ABANDONED_0 + nCount– 1) | If bWaitAll is TRUE, the return value indicates that the state of all specified objects is signaled and at least one of the objects is an abandoned mutex object. 【如果bWaitAll为TRUE,则返回值表示所有指定对象的状态都有信号,并且至少有一个对象是放弃的互斥对象。】 If bWaitAll is FALSE, the return value minus WAIT_ABANDONED_0 indicates the lpHandles array index of an abandoned mutex object that satisfied the wait. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled. 【如果bWaitAll为FALSE,则返回值减去WAIT_ABANDONED_0 表示满足等待的废弃互斥对象的lpHandles数组索引。互斥对象的所有权被授予调用线程,互斥被设置为nonsignaled。】 If a mutex was protecting persistent state information, you should check it for consistency. 【如果互斥体正在保护持久状态信息,则应检查其一致性。】 |
WAIT_TIMEOUT 0x00000102L | The time-out interval elapsed and the conditions specified by the bWaitAll parameter are not satisfied. 【超时间隔已过,bWaitAll参数指定的条件不满足。】 |
If the function fails, the return value is WAIT_FAILED ((DWORD)0xFFFFFFFF). To get extended error information, call GetLastError.
【如果函数失败,则返回值为WAIT_FAILED ((DWORD)0xFFFFFFFF)。要获取扩展错误信息,请调用GetLastError。】
Remarks
The WaitForMultipleObjects function determines whether the wait criteria have been met. If the criteria have not been met, the calling thread enters the wait state until the conditions of the wait criteria have been met or the time-out interval elapses.
【WaitForMultipleObjects函数确定是否满足等待条件。如果未满足条件,则调用线程将进入等待状态,直到满足等待条件或超时间隔结束。】
When bWaitAll is TRUE, the function’s wait operation is completed only when the states of all objects have been set to signaled. The function does not modify the states of the specified objects until the states of all objects have been set to signaled. For example, a mutex can be signaled, but the thread does not get ownership until the states of the other objects are also set to signaled. In the meantime, some other thread may get ownership of the mutex, thereby setting its state to nonsignaled.
【当bWaitAll为TRUE时,只有当所有对象的状态都设置为signaled时,函数的等待操作才完成。函数不会修改指定对象的状态,直到所有对象的状态都设置为signaled。例如,可以向互斥体发送信号,但是线程在其他对象的状态也设置为signaled之前不会获得所有权。同时,其他线程可能会获得互斥锁的所有权,从而将其状态设置为nonsignaled。】
When bWaitAll is FALSE, this function checks the handles in the array in order starting with index 0, until one of the objects is signaled. If multiple objects become signaled, the function returns the index of the first handle in the array whose object was signaled.
【当bWaitAll为FALSE时,此函数将按从索引0开始的顺序检查数组中的句柄,直到向其中一个对象被设置为signaled为止。如果多个对象被设置为signaled,函数将返回数组中第一个句柄的索引,该句柄的对象已被设置为signaled。】
The function modifies the state of some types of synchronization objects. Modification occurs only for the object or objects whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one. For more information, see the documentation for the individual synchronization objects.
【此函数用于修改某些类型的同步对象的状态。仅对其信号状态导致函数返回的一个或多个对象进行修改。例如,信号量对象的计数减少1。有关详细信息,请参阅各个同步对象的文档。】
To wait on more than MAXIMUM_WAIT_OBJECTS handles, use one of the following methods:
【要等待超过MAXIMUM_WAIT_OBJECTS个对象句柄,请使用以下方法之一:】
1.Create a thread to wait on MAXIMUM_WAIT_OBJECTS handles, then wait on that thread plus the other handles. Use this technique to break the handles into groups of MAXIMUM_WAIT_OBJECTS.
【创建一个线程以等待MAXIMUM_WAIT_OBJECTS个对象句柄,然后等待该线程和其他句柄。使用此技术可以将句柄分成MAXIMUM_WAIT_OBJECTS对象组。】
2.Call RegisterWaitForSingleObject to wait on each handle. A wait thread from the thread pool waits on MAXIMUM_WAIT_OBJECTS registered objects and assigns a worker thread after the object is signaled or the time-out interval expires.
【调用RegisterWaitForSingleObject等待每个句柄。线程池中的等待线程等待已注册对象的MAXIMUM_WAIT_OBJECTS个对象,并在对象发出信号或超时间隔过期后分配工作线程。】
The WaitForMultipleObjects function can specify handles of any of the following object types in the lpHandles array:
【WaitForMultipleObjects函数可以在lpHandles数组中指定以下任何对象类型的句柄:】
Change notification 【更改通知】
Console input 【控制台输入】
Event 【事件】
Memory resource notification 【内存资源通知】
Mutex 【互斥】
Process 【进程】
Semaphore 【信号量】
Thread 【线程】
Waitable timer 【等待计时器】
Use caution when calling the wait functions and code that directly or indirectly creates windows. If a thread creates any windows, it must process messages. Message broadcasts are sent to all windows in the system. A thread that uses a wait function with no time-out interval may cause the system to become deadlocked. Two examples of code that indirectly creates windows are DDE and the CoInitialize function. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than WaitForMultipleObjects.
【在调用直接或间接创建窗口的等待函数和代码时要小心。如果线程创建任何窗口,它必须处理消息。消息广播发送到系统中的所有窗口。使用无超时间隔的等待函数的线程可能会导致系统死锁。间接创建窗口的两个代码示例是DDE和CoInitialize函数。因此,如果有创建窗口的线程,请使用MsgWaitForMultipleObjects或MsgWaitForMultipleObjectsEx,而不是WaitForMultipleObjects。】
这篇关于MFC WaitForMultipleObjects 函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!