Chapter09-"内核模式下的线程同步"之等待函数
来源:互联网 发布:不可抗力网络剧百度云 编辑:程序博客网 时间:2024/05/29 15:28
概述:
用户模式下的线程同步(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.
- Chapter09-"内核模式下的线程同步"之等待函数
- Chapter09-“内核模式下的线程同步”之可等待的计时器内核对象
- Chapter09-"内核模式下的线程同步"之事件内核对象
- Chapter09-“内核模式下的线程同步”之信号量内核对象
- Chapter09-“内核模式下的线程同步”之互斥量内核对象
- windows编程之内核模式下的线程同步
- -【内核对象线程同步】等待函数
- Windows 内核模式下的线程同步
- 线程同步(2) - 内核模式下的线程同步
- 线程同步(2) - 内核模式下的线程同步
- 内核模式下线程同步
- 【Windows】线程漫谈——线程同步之等待函数和事件内核对象
- Windows Via C/C++:内核模式下的线程同步——WaitForSingleObject/WaitForMultipleObjects函数
- Windows Via C/C++:内核模式下的用户同步——成功等待的副作用
- Windows内核模式的线程同步之事件(Event)
- -【内核对象线程同步】成功等待的副作用
- 【内核对象线程同步】成功等待的副作用
- 线程同步之等待句柄
- 《python编程入门经典》学习笔记–第八章(一)
- JQuery动态操作表格(添加、删除、多选、全选删除行操作)
- AndEngine编译
- 感《明朝后期的中缅战争》
- 练习题
- Chapter09-"内核模式下的线程同步"之等待函数
- 几道水博弈
- POJ 2152(树形DP)
- 未能找到类型或命名空间名称“XXXX”(是否缺少 using 指令或程序集引用?)
- 阻止局域网中的ARP攻击
- 黑马程序员_事件
- XML简明教程(4)
- ubuntu 安装
- 【字典树】 hdu1251 统计难题