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.

  • 相关阅读:
    阿里知识图谱首次曝光:每天千万级拦截量,亿级别全量智能审核
    LSTM简介以及数学推导(FULL BPTT)
    深度学习算法索引(持续更新)
    学界 | Yann LeCun新作,中日韩文本分类到底要用哪种编码?
    Android 常见内存泄漏的解决方式
    集成支付宝支付
    【4.29安恒杯】writeup
    sdut 3-7 类的友元函数的应用
    Linux下libsvm的安装及简单练习
    iOS 使用腾讯地图显示用户位置注意事项
  • 原文地址:https://www.cnblogs.com/java20130722/p/3207199.html
Copyright © 2011-2022 走看看