MFC WaitForMultipleObjects 函数

2024-02-09 12:48

本文主要是介绍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

nCountThe 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。
lpHandlesAn 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.
【句柄必须具有同步访问权限。有关详细信息,请参阅标准访问权限。】
bWaitAllIf 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时,函数返回。在后一种情况下,返回值指示其状态导致函数返回的对象。】
dwMillisecondsThe 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/valueDescription
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 函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/694271

相关文章

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

kotlin的函数forEach示例详解

《kotlin的函数forEach示例详解》在Kotlin中,forEach是一个高阶函数,用于遍历集合中的每个元素并对其执行指定的操作,它的核心特点是简洁、函数式,适用于需要遍历集合且无需返回值的场... 目录一、基本用法1️⃣ 遍历集合2️⃣ 遍历数组3️⃣ 遍历 Map二、与 for 循环的区别三、高

C语言字符函数和字符串函数示例详解

《C语言字符函数和字符串函数示例详解》本文详细介绍了C语言中字符分类函数、字符转换函数及字符串操作函数的使用方法,并通过示例代码展示了如何实现这些功能,通过这些内容,读者可以深入理解并掌握C语言中的字... 目录一、字符分类函数二、字符转换函数三、strlen的使用和模拟实现3.1strlen函数3.2st

MySQL中COALESCE函数示例详解

《MySQL中COALESCE函数示例详解》COALESCE是一个功能强大且常用的SQL函数,主要用来处理NULL值和实现灵活的值选择策略,能够使查询逻辑更清晰、简洁,:本文主要介绍MySQL中C... 目录语法示例1. 替换 NULL 值2. 用于字段默认值3. 多列优先级4. 结合聚合函数注意事项总结C