03等待多个线程返回WaitForMultipleObject

二. WaitForMultipleObject 等待单个线程返回

1. 函数原型
DWORD WINAPI WaitForMultipleObjects(
  _In_       DWORD   nCount,
  _In_ const HANDLE  *lpHandles,
  _In_       BOOL    bWaitAll,
  _In_       DWORD   dwMilliseconds
);
2. 参数说明
  • 第一个参数 nCount 为等待的内核对象个数,可以是 0 到 MAXIMUM_WAIT_OBJECTS (64)中的一个值。
  • 第二个参数 lpHandles 为一个存放被等待的内核对象句柄的数组。
  • 第三个参数 bWaitAll 是否等到所有内核对象为已通知状态后才返回,如果为 TRUE,则只有当等待的所有内核对象为已通知状态时函数才返回,如果为 FALSE,则只要一个内核对象为已通知状态,则该函数返回。
  • 第四个参数 dwMilliseconds 为等待时间,和 WaitForSingleObject 中等待 dwMilliseconds 参数类似。
3. 返回值
  • WAIT_ABANDONED_0 表示所有对象都发出消息,而且其中有一个或对个属于互斥体(一旦拥有他们的进程结束,就会发出信号)
  • WAIT_TIMEOUT 对象保持未发出信号的状态,但规定的等待超时时间已经超过
  • WAIT_OBJECT_0 所有对象都发出信号
  • WAIT_FAILED 表示函数执行失败,会设置错误码,错误码可通过 GetLastError 获取
  • WAIT_IO_COMPLETION (仅适用于 WaitForMultipleObjectsEx )由于一个 I/O 完成操作已做好准备执行,所以造成了函数的返回值
    —— 如果 bWaitAll 为 FALSE,那么返回结果相似,只是可能还会返回相对于 WAIT_ABANDONED_0 或 WAIT_OBJECT_0 的一个正偏移量,指出哪个对象时被抛弃还是发出信号。
    补充: WAIT_OBJECT_0 时微软定义的一个宏,就可以把这个宏当做一个数字。

例如, 返回值 WAIT_OBJECT_0 + 5 意味着列表中的第 5 个对象发出了信号。当 bWaitAll 参数为 FALSE 时可以等待其中之一的信号。

4. 示例

(1)参数 bWaitAll 为 FALSE,只要一个内核对象为已通知状态,则该函数返回。

#include <Windows.h>
#include <stdio.h>

const unsigned int THREAD_NUM = 3;
DWORD WINAPI  ThreadFunc(LPVOID p)
{
	printf("I am a child thread 0 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
	Sleep(500);
	printf("The child thread 0 which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

DWORD WINAPI  ThreadFunc1(LPVOID p)
{
	printf("I am a child thread 1 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
	Sleep(2000);
	printf("The child thread 1 which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

DWORD WINAPI  ThreadFunc2(LPVOID p)
{
	printf("I am a child thread 2 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
	Sleep(4000);
	printf("The child thread 2 which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

int main()
{
	printf("I am the main thread that pid is %d ...\n", GetCurrentThreadId());  //输出主线程pid
	HANDLE hThread[THREAD_NUM];
	hThread[0] = CreateThread(NULL, 0, ThreadFunc, 0, 0, NULL); // 创建线程
	hThread[1] = CreateThread(NULL, 0, ThreadFunc1, 0, 0, NULL); // 创建线程
	hThread[2] = CreateThread(NULL, 0, ThreadFunc2, 0, 0, NULL); // 创建线程

	DWORD resulut = WaitForMultipleObjects(THREAD_NUM, hThread, FALSE, INFINITE); //只要有一个线程返回就结束
	if (WAIT_OBJECT_0 == resulut)
	{
		printf("The child thread 0 finished waiting ...\n");
	}
	else if (WAIT_OBJECT_0 + 1 == resulut)
	{
		printf("The child thread 1 finished waiting ...\n");
	}
	else if (WAIT_OBJECT_0 + 2 == resulut)
	{
		printf("The child thread 2 finished waiting ...\n");
	}
	else if (WAIT_FAILED == resulut)
	{
		printf("The fuction WaitForMultipleObjects error! \n");
	} 
	printf("The main thread which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

输出结果为:

image

—— 如果将代码中的 “Thread 0” 的 Sleep 时间加长,让 “Thread 1” 先退出,则 会进入 WAIT_OBJECT_0 + 1 == resulut 分支,打印 “The child thread 1 finished waiting ...”,然后退出主线程。这样就可以通过返回值判断是哪个对象发送的信号。

(2)参数 bWaitAll 为 TRUE,等待所有线程返回。

#include <Windows.h>
#include <stdio.h>

const unsigned int THREAD_NUM = 3;
DWORD WINAPI  ThreadFunc(LPVOID p)
{
	printf("I am a child thread 0 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
	Sleep(500);
	printf("The child thread 0 which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

DWORD WINAPI  ThreadFunc1(LPVOID p)
{
	printf("I am a child thread 1 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
	Sleep(2000);
	printf("The child thread 1 which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

DWORD WINAPI  ThreadFunc2(LPVOID p)
{
	printf("I am a child thread 2 which pid is %d ...\n", GetCurrentThreadId());   //输出子线程pid
	Sleep(4000);
	printf("The child thread 2 which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

int main()
{
	printf("I am the main thread that pid is %d ...\n", GetCurrentThreadId());  //输出主线程pid
	HANDLE hThread[THREAD_NUM];
	//创建第一个子线程
	hThread[0] = CreateThread(NULL, 0, ThreadFunc, 0, 0, NULL); // 创建线程
	hThread[1] = CreateThread(NULL, 0, ThreadFunc1, 0, 0, NULL); // 创建线程
	hThread[2] = CreateThread(NULL, 0, ThreadFunc2, 0, 0, NULL); // 创建线程

	int wait_num = 0;
	while (wait_num < THREAD_NUM)
	{
		DWORD resulut = WaitForMultipleObjects(THREAD_NUM, hThread, TRUE, INFINITE); //所有子线程返回才结束
		if (WAIT_OBJECT_0 <= resulut <= WAIT_OBJECT_0 + 2)
		{
			printf("The Return value is %d, One child thread finished waiting ...\n", resulut);
			wait_num++;
		}	
	}

	printf("The main thread which pid is %d quit ...\n", GetCurrentThreadId());
	return 0;
}

输出结果为:

image

posted @ 2018-07-26 15:07  洛克十年  阅读(401)  评论(0编辑  收藏  举报