WaitForMultipleObjects是Windows中的一个功能非常强大的函数,几乎可以等待Windows中的所有的内核对象 函数原型为: DWORD WaitForMultipleObjects DWORD WINAPI MyThreadProc(LPVOID lpParam) { while(TRUE) { //每次等500毫秒 int nIndex = WaitForMultipleObjects 等待所有的事件 DWORD WINAPI MyThreadProc(LPVOID lpParam) while(TRUE) { //每次等500毫秒 nt nIndex = WaitForMultipleObjects
hthreads[i] = CreateThread(nullptr, 0, Thread, nullptr, 0, nullptr); //等待多个线程被触发 dres = WaitForMultipleObjects //超时 break; case WAIT_FAILED: //FAILED break; } dres = WaitForMultipleObjects ] = CreateThread(nullptr, 0, Thread2, nullptr, 0, nullptr); SetEvent(g_event);//触发事件 dres = WaitForMultipleObjects
互斥量包含的几个操作原语: CreateMutex() 创建一个互斥量 OpenMutex() 打开一个互斥量 ReleaseMutex() 释放互斥量 WaitForMultipleObjects CreateEvent() 创建一个事件 OpenEvent() 打开一个事件 SetEvent() 回置事件 WaitForSingleObject() 等待一个事件 WaitForMultipleObjects () 等待多个事件 WaitForMultipleObjects 函数原型: WaitForMultipleObjects( IN DWORD nCount
如果需要在一个线程中等待多个事件,则用WaitForMultipleObjects()来等待。 WaitForMultipleObjects()与WaitForSingleObject()类似,同时监视位于句柄数组中的所有句柄。 WaitForMultipleObjects()的函数原型为: DWORD WaitForMultipleObjects( DWORD nCount, // 等待句柄数 CONST HANDLE *lpHandles 下面给出的代码主要展示了对WaitForMultipleObjects()函数的使用。 WaitForSingleObject()和WaitForMultipleObjects()主要用在试图进入共享资源的线程函数入口处,主要用来判断信号量的当前可用资源计数是否允许本线程的进入。
使用WaitForMultipleObjects函数来等待所有进程完成。 NUM_PROCESSES; i++) { hEvents[i] = pi[i].hProcess; } // 等待所有进程完成 waitResult = WaitForMultipleObjects (NUM_PROCESSES, hEvents, TRUE, INFINITE); if (waitResult == WAIT_FAILED) { printf("WaitForMultipleObjects 使用WaitForMultipleObjects函数来等待所有三个进程完成。这个函数会阻塞,直到所有指定的事件(在这里是进程句柄)都被满足,即所有进程都已退出。
); // 等候线程结束 关闭线程句柄 (是关闭句柄,不是关闭线程,线程没结束,关闭句柄线程照样执行) CloseHandle 等候多个对象事件 HANDLE hAndle[2] = {NULL}; WaitForMultipleObjects
RtlCreateHeap', 'LdrLoadDll', 'RtlMoveMemory', 'RtlCopyMemory', 'SetPropA', 'WaitForSingleObjectEx', 'WaitForMultipleObjects NtWaitForSingleObject', 'CreateWindowExA', 'RegisterHotKey', 'timeSetEvent', 'IcmpSendEcho', 'WaitForSingleObjectEx', 'WaitForMultipleObjects
hThreadHand[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThreadFun2, NULL, 0, NULL); //创建两个线程 WaitForMultipleObjects GetLastError()); return 1; } } // Wait for all threads to terminate WaitForMultipleObjects NULL); hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThreadFun2, 0, 0, NULL); WaitForMultipleObjects NULL); hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThreadFun2, 0, 0, NULL); WaitForMultipleObjects NULL); hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MyThreadFun2, 0, 0, NULL); WaitForMultipleObjects
::CreateThread( NULL, 0, ThreadNormal, NULL, 0, &dwThreadId );//创建的线程默认优先级为Normal ::WaitForMultipleObjects
WaitForMultipleObjects函数用于等待多个内核对象。(在实例PriorityDemo中最后有所展示。) 22. Windows Explorer进程中的线程就是在高优先级下运行的。 NULL, 0, ThreadNormal, NULL, 34 0, &dwThreadID); 35 36 // 等待两个线程内核对象都变成受信状态 37 ::WaitForMultipleObjects } 48 49 /* 50 HANDLE h[2]; 51 h[0] = hThread1; 52 h[1] = hThread2; 53 DWORD dw = ::WaitForMultipleObjects (2, h, FALSE, 5000); 54 switch(dw) 55 { 56 case WAIT_FAILED: 57 // 调用WaitForMultipleObjects
0, NULL); hThreads[1] = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL); // 等待所有线程结束 WaitForMultipleObjects eventTriggered) { printf("5秒内事件未触发\n"); } CloseHandle(hEvent); return 0;}五、高级应用与最佳实践5.1 与WaitForMultipleObjects 的对比函数特点适用场景WaitForSingleObject等待单个对象简单同步需求WaitForMultipleObjects等待多个对象复杂同步,如同时等待多个事件 使用建议:当需要等待多个对象时, 优先使用WaitForMultipleObjects,避免循环调用WaitForSingleObject导致的效率问题。
SetEvent() 设置信号 PulseEvent() 重置信号-设置信号-重置信号,使信号状态发生一次脉冲变化 ResetEvent() 重置信号 WaitForSingleObject 等待信号 WaitForMultipleObjects
NULL,//线程函数的参数 0,//立即启动 &uThreadId//内核给新生成的线程分配的线程ID ); ::Sleep(600); g_bContinue = FALSE; ::WaitForMultipleObjects
(NULL,0, ThreadFun2,(PVOID)threadID1,0, NULL); HANDLE hThread[] = { hThread1 ,hThread2 }; WaitForMultipleObjects
= (HANDLE)_beginthread(threadProc2, 0, “B:”); HANDLE hThread[] = { hThread1 ,hThread2 }; WaitForMultipleObjects
; ExitProcess(3); } } // 等待所有线程结束 WaitForMultipleObjects(MAX_THREADS
更深层次的原因,就是 Windows API 的 WaitForMultipleObjects 所支持的最大句柄数是 MAXIMUM_WAIT_OBJECTS,即 64。
例如: win上通过thread+WaitForMultipleObjects的方式来对接到poller unix上通过thread/waitpid的方式来对接到poller 相关poller接口主要有下面四个
当使用WaitForSingleObject或者WaitForMultipleObjects函数等待事件对象时,会阻塞线程直到事件状态被置位。 each, 0, &ThreadCount); // 等待线程事件被触发 WaitForSingleObject(g_hThreadEvent, INFINITE); } WaitForMultipleObjects 可以通过SetEvent函数将事件对象设置为signaled状态,通过ResetEvent函数将事件对象设置为non-signaled状态,也可以通过WaitForSingleObject或者WaitForMultipleObjects 可以通过ReleaseSemaphore函数将该信号量对象的计数加1,通过WaitForSingleObject或者WaitForMultipleObjects函数等待信号量对象的计数变成正数以后再将其减 可以通过WaitForSingleObject或者WaitForMultipleObjects函数等待互斥体对象,以确保只有一个线程能够访问共享资源,其他线程需要等待该线程释放互斥体对象后才能继续访问。
当使用WaitForSingleObject或者WaitForMultipleObjects函数等待事件对象时,会阻塞线程直到事件状态被置位。 可以通过SetEvent函数将事件对象设置为signaled状态,通过ResetEvent函数将事件对象设置为non-signaled状态,也可以通过WaitForSingleObject或者WaitForMultipleObjects 可以通过ReleaseSemaphore函数将该信号量对象的计数加1,通过WaitForSingleObject或者WaitForMultipleObjects函数等待信号量对象的计数变成正数以后再将其减 可以通过WaitForSingleObject或者WaitForMultipleObjects函数等待互斥体对象,以确保只有一个线程能够访问共享资源,其他线程需要等待该线程释放互斥体对象后才能继续访问。 else { tHandle[x] = CreateThread(NULL, 0, FuncB, NULL, 0, NULL); } } // 等待所有线程函数执行完毕 WaitForMultipleObjects