zoukankan      html  css  js  c++  java
  • Chapter09"内核模式下的线程同步"之等待函数

    概述:

           用户模式下的线程同步(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.

  • 相关阅读:
    【WP开发】记录屏幕操作
    【.NET深呼吸】清理对象引用,有一个问题容易被忽略
    【WP开发】JSON数据的读与写
    【WP8.1开发】RenderTargetBitmap类的特殊用途
    【WP 8.1开发】How to 图像处理
    【WP8.1开发】用手机来控制电脑的多媒体播放
    【WP 8.1开发】如何动态生成Gif动画
    【WP8.1开发】基于应用的联系人存储
    使用awk处理文本
    PHP数组和字符串的处理函数汇总
  • 原文地址:https://www.cnblogs.com/java20130722/p/3207199.html
Copyright © 2011-2022 走看看