概述:
用户模式下的线程同步(user-mode thread synchronization)一个明显的优点是速度快;如果你非常在意程序的性能,则最好使用用户模式下的线程同步。但是用户模式下也有一定的缺点限制:interlocked lock APIs只能对一个值进行操控,而不能使得一个线程处于等待状态;你可以用Critical Section系列函数使某个线程处于等待状态,但是你稍有不慎则有可能陷入死锁。
所以在某些情况下,内核模式的线程同步(kernel-mode thread synchronization)是一个不错的选择。
等待的函数(waitfunctions)
DWORD WaitForSingleObject( HANDLEhObject, DWORD dwMilliseconds);
该函数的功能是让当前线程处于等待状态,直到hObject对应的内核对象变成触发状态,或者是等待超过dwMilliseconds设置的时间片。
代码示例:
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;
}
WaitForSingleObject可以等待以下对象(objects):
- Event
- Mutex
- Semaphore
- Process
- Thread
WaitForSingleObject的返回值:
Value
|
Description
|
WAIT_OBJECT_0
|
The state of the specified object is signaled.
|
WAIT_TIMEOUT
|
The time-out interval elapsed, and the object's state is nonsignaled.
|
WAIT_FAILED
|
indicates failure. Waiting on an invalid handle causes WaitForSingleObject to return WAIT_FAILED.
|
DWORDWaitForMultipleObjects( DWORD nCount, CONST HANDLE* lpHandles, BOOL fWaitAll, DWORDdwMilliseconds )
该函数的功能是阻止当前进程;如果fWaitAll参数值为TRUE,则plHandles数组内的所有对象变成触发状态,函数才返回让当前进程继续运行;如果fWaitAll参数值为FALSE,则plHandles数组内有一个对象变成触发状态,函数就立即返回使当前线程继续运行。
代码示例:
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;
}
WaitForMultipleObjects可以等待的对象与WaitForSingleObject一样,其返回值也相似,如下表:
Value
|
Description
|
WAIT_OBJECT_0 to (WAIT_OBJECT_0 + nCount –1)
|
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.
|
WAIT_TIMEOUT
|
The time-out interval elapsed and the condition specified by the fWaitAll parameter is not satisfied.
|
WAIT_FAILED
|
indicates failure. To get extended error information, call
GetLastError.
|
分享到:
相关推荐
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
Chapter3-《Ext JS高级程序设计》源代码.rar Chapter4-《Ext JS高级程序设计》源代码.rar Chapter5《Ext JS高级程序设计》源代码.rar Chapter6-《Ext JS高级程序设计》源代码.rar Chapter7-《Ext JS高级程序设计》源...
操作系统概念英文课件:Chapter 09-virtual memory.pptx
CISSP ALL IN ONE 5TH EDITON - Part 3/3 ( Chapter09 - 12)
ProWPFinVB-src-Chapter02-15 ProWPFinVB-src-Chapter02-15 ProWPFinVB-src-Chapter02-15
ProWPFinVB-src-Chapter15-28ProWPFinVB-src-Chapter15-28ProWPFinVB-src-Chapter15-28
电力系统自动化chapter2-2同步发电机励磁系统.pptx
以下是一些典型函数的举例,全部的内置函数,可以参考官网介绍。比较函数逻辑函数算术函数字符串函数时间函数聚合函数SUM([ ALL | DISTINCT ] ex
嵌入式系统软件基础描述及各类嵌入式软件分析方法。嵌入式系统及应用-Chapter3-嵌入式软件系统基础
chapter14-cross coupling-bc [兼容模式].pdf
计算机网络第6版课件:Chapter 4-4 routing and IP routing.ppt
chapter9--network--server.ipynb
《Android开发案例驱动教程》全面介绍了在开源的手机平台Android操作系统下的应用程序开发技术,包括UI、多线程、数据存储、多媒体、云端应用以及通信应用等方面。 《Android开发案例驱动教程》采用案例驱动模式展开...
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/759473