weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
          數(shù)據(jù)加載中……

          .Net 下信號量(Semaphore)的一種實現(xiàn)

          動機


          ??? 從開始接觸多線(進)程編程模型開始,學習的就是和信號量(Semaphore)相關的同步原語。不知道為什么
          .Net Framework 里卻沒有相應的東東。要命的是, 我以前有很多久經(jīng)考驗的C++代碼都是用她來實現(xiàn)的, 為了不使革命先烈的藥白吃, 血白流, 只好自己生一個了。

          ?


          什么是信號量(Semaphore)

          ??? 如果你已經(jīng)了解信號量(Semaphore)的概念了,請?zhí)^這一段。
          ????
          ????信號量(Semaphore)是在多線程環(huán)境下使用的一種設施, 它負責協(xié)調(diào)各個線程, 以保證它們能夠正確、合理的使用公共資源

          ??? 我們來看看一個停車場是怎樣運作的。為了簡單起見,假設停車場只有三個車位,一開始三個車位都是空的。這是如果同時來了五輛車,看門人允許其中三輛不受阻礙的進入,然后放下車攔,剩下的車則必須在入口等待,此后來的車也都不得不在入口處等待。這時,有一輛車離開停車場,看門人得知后,打開車攔,放入一輛,如果又離開兩輛,則又可以放入兩輛,如此往復。

          ?

          ??? 在這個停車場系統(tǒng)中,車位是公共資源,每輛車好比一個線程,看門人起的就是信號量的作用。

          ??? 更進一步,信號量的特性如下:信號量是一個非負整數(shù)(車位數(shù)),所有通過它的線程(車輛)都會將該整數(shù)減一(通過它當然是為了使用資源),當該整數(shù)值為零時,所有試圖通過它的線程都將處于等待狀態(tài)。在信號量上我們定義兩種操作:
          Wait (等待) Release (釋放)。 當一個線程調(diào)用 Wait等待)操作 時,它要么通過然后將信號量減一,要么一自等下去,直到信號量大于一或超時。 Release(釋放) 實際上是在信號量上執(zhí)行加操作,對應于車輛離開停車場,該操作之所以叫做“釋放”是應為加操作實際上是釋放了由信號量守護的資源。

          ?

          實現(xiàn)
          ?

          ???大家都知道, .Net Framework類庫中 提供的線程同步設施包括:

          ??? Monitor AutoResetEvent ManualResetEvent Mutex ReadWriteLock InterLock 其中 AutoResetEvent ManualResetEvent Mutex 派生自 WaitHandler ,它們實際上是封裝了操作系統(tǒng)提供的內(nèi)核對象。而其它的應當是在 .Net 虛擬機中土生土長的。顯然來自操作系統(tǒng)內(nèi)核對象的設施使用起來效率要差一些。不過效率并不是我們這里要考慮的問題,我們將使用 兩個 Monitor 一個M anualResetEvent 對象來模擬一個信號量。


          代碼如下:

          ???? public ? class ?Semaphore
          ????
          {
          ????????
          private ?ManualResetEvent?waitEvent? = ? new ?ManualResetEvent( false );
          ????????
          private ? object ?syncObjWait? = ? new ? object ();
          ????????
          private ? int ?????maxCount? = ? 1 ;?????? // 最大資源數(shù)
          ???????? private ? int ?????currentCount? = ? 0 ;?? // 當前資源數(shù)

          ????????
          public ?Semaphore()
          ????????
          {

          ????????}


          ????????
          public ?Semaphore(? int ?maxCount?)
          ????????
          {
          ????????????
          this .maxCount? = ?maxCount;
          ????????}


          ????????
          public ? bool ?Wait()
          ????????
          {
          ????????????
          lock (?syncObjWait?)?????? // 只能一個線程進入下面代碼
          ???????????? {
          ????????????????
          bool ?waitResult? = ? this .waitEvent.WaitOne();??? // 在此等待資源數(shù)大于零
          ???????????????? if (?waitResult?)
          ????????????????
          {
          ????????????????????
          lock (? this ?)
          ????????????????????
          {
          ????????????????????????
          if (?currentCount? > ? 0 ?)
          ????????????????????????
          {
          ????????????????????????????currentCount
          -- ;
          ????????????????????????????
          if (?currentCount? == ? 0 ?)
          ????????????????????????????
          {
          ????????????????????????????????
          this .waitEvent.Reset();
          ????????????????????????????}


          ????????????????????????}

          ????????????????????????
          else
          ????????????????????????
          {
          ????????????????????????????System.Diagnostics.Debug.Assert(?
          false ,? " Semaphore?is?not?allow?current?count?<?0 " ?);
          ????????????????????????}

          ????????????????????}

          ????????????????}

          ????????????????
          return ?waitResult;
          ????????????}

          ????????}


          ????????
          /// ? <summary>
          ????????
          /// ?允許超時返回的?Wait?操作
          ????????
          /// ? </summary>
          ????????
          /// ? <param?name="millisecondsTimeout"></param>
          ????????
          /// ? <returns></returns>

          ???????? public ? bool ?Wait(? int ?millisecondsTimeout?)
          ????????
          {
          ????????????
          lock (?syncObjWait?)?? // ?Monitor?確保該范圍類代碼在臨界區(qū)內(nèi)
          ???????????? {
          ????????????????
          bool ?waitResult? = ? this .waitEvent.WaitOne(millisecondsTimeout, false );
          ????????????????
          if (?waitResult?)
          ????????????????
          {
          ????????????????????
          lock (? this ?)
          ????????????????????
          {
          ????????????????????????
          if (?currentCount? > ? 0 ?)
          ????????????????????????
          {
          ????????????????????????????currentCount
          -- ;
          ????????????????????????????
          if (?currentCount? == ? 0 ?)
          ????????????????????????????
          {
          ????????????????????????????????
          this .waitEvent.Reset();
          ????????????????????????????}


          ????????????????????????}

          ????????????????????????
          else
          ????????????????????????
          {
          ????????????????????????????System.Diagnostics.Debug.Assert(?
          false ,? " Semaphore?is?not?allow?current?count?<?0 " ?);
          ????????????????????????}

          ????????????????????}

          ????????????????}

          ????????????????
          return ?waitResult;
          ????????????}

          ????????}



          ????????
          public ? bool ?Release()
          ????????
          {
          ????????????
          lock (? this ?)? // ?Monitor?確保該范圍類代碼在臨界區(qū)內(nèi)
          ???????????? {
          ????????????????currentCount
          ++ ;
          ????????????????
          if (?currentCount? > ? this .maxCount?)
          ????????????????
          {
          ????????????????????currentCount?
          = ? this .maxCount;
          ????????????????????
          return ? false ;
          ????????????????}

          ????????????????
          this .waitEvent.Set();?? // 允許調(diào)用Wait的線程進入
          ????????????}

          ????????????
          return ? true ;
          ????????}


          ????}

          from: http://www.cnblogs.com/louisliu/archive/2006/02/15/331424.aspx

          posted on 2007-01-05 23:06 weidagang2046 閱讀(673) 評論(0)  編輯  收藏 所屬分類: Windows

          主站蜘蛛池模板: 丹阳市| 遂溪县| 松潘县| 微山县| 金堂县| 陆川县| 逊克县| 黄骅市| 什邡市| 榆树市| 平顺县| 鄢陵县| 海门市| 新宾| 女性| 三江| 迁西县| 军事| 宣化县| 禄丰县| 柳州市| 左贡县| 内江市| 东乡族自治县| 靖州| 建水县| 四会市| 肇庆市| 镇康县| 赤水市| 岑巩县| 卢氏县| 宣武区| 南江县| 讷河市| 托克托县| 岳池县| 略阳县| 新昌县| 无棣县| 康马县|