zoukankan      html  css  js  c++  java
  • 《Win32多线程程序设计》学习笔记 第4章 同步控制之 信号量(Semaphores)

    win32中的一个semaphore可以被锁住最多n次,其中n是semaphore被产生时指定的。n常常被设计成用来代表“可以锁住一份资源”的线程资源,不过并非单独一个线程就不能拥有所有的锁定。理论上,mutex是semaphore的一种退化,如果你产生一个semaphore并令最大值为1,那就是一个mutex。

     产生信号量(semaphore)

    代码
    Creates or opens a named or unnamed semaphore object.To specify an access mask for the object, use the CreateSemaphoreEx function.

    HANDLE WINAPI CreateSemaphore(
     LPSECURITY_ATTRIBUTES lpSemaphoreAttributes,  
    //安全属性
     LONG lInitialCount,    //The initial count for the semaphore object. This value must be greater than or equal to zero and less than or equal to lMaximumCount. 
     LONG lMaximumCount,    //The maximum count for the semaphore object. must be greater than zero.
     LPCTSTR lpName         //semaphore的名称,任何线程或进程都可以用这个名称应用到这个semaphore,可以为NUll。
    );
    返回值:如果成功传回一个handle,否则为NULL,如果指定的semaphore名称已经存在,该函数还是会成功的,GetLastError会传回ERROR_ALREADY_EXIST.

    获得锁定

     可以使用任何一种wait...函数要求锁定一个semaphore。如果semaphore的限值不为0,wait()...函数会立刻返回。一旦semaphore的现值降为0,就表示资源已经耗尽,此时,任何线程如果调用wait...()函数,就必然要等待,知道某个锁定被解除为止。

    解除锁定

     调用ReleaseSemaphore解除锁定。

    Increases the count of the specified semaphore object by a specified amount.

    BOOL WINAPI ReleaseSemaphore(
      HANDLE hSemaphore,       
    //A handle to the semaphore object. 
      LONG lReleaseCount,      //Semaphore现值的增额,该值不可以为负值或0
      LPLONG lpPreviousCount   //传回semaphore的原来的值
    );

    返回值:如果成功,传回TRUE

    lpPreviousCount   所传的值是一个瞬间值。不可以把lReleaseCount加上*lpPreviousCount   当做是semaphore的现值,因为其他线程可能已经改变了semaphore的值。

     与mutex不同,调用ReleaseSemaphore的那个线程,并不一定就是得调用Wait...()的那个线程。任何线程都可以在任何时间调用ReleaseSemaphore(),解除被任何线程锁定的semaphore。

     MSDN中有个使用semaphore的例子,可以看一下

  • 相关阅读:
    知乎 : 有什么你认为很简单的问题实际的证明却很复杂?
    民科吧 的 一题 : ∂ f / ∂ x =
    知乎: Lurie 的 derived algebraic geometry 有多重要?
    说说 网友 专业证伪 的 那道 几何题
    在 《数学问题,连接两个点的曲线旋转所成曲面中,面积最小的曲线是什么?》 里 的 讨论
    证明 夹逼定理 和 洛必达法则
    一道数学题 : 数列 { bn } 收敛, 证明 { an } 也收敛
    一道数学题 : f ( 2^x ) + f ( 3^x ) = x , 求 f ( x )
    网友 lzmsunny96 的 一个 分数 分解题
    我 搞了一个 尺规作图 不能 实现 三等分角 的 证明
  • 原文地址:https://www.cnblogs.com/kwliu/p/2195916.html
Copyright © 2011-2022 走看看