• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            沒畫完的畫

            喂馬 劈柴 BBQ~
            posts - 37, comments - 55, trackbacks - 0, articles - 0
              C++博客 ::  :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            history:
            08.08.27   修改部份翻譯不當(dāng)?shù)牡胤?/span>   網(wǎng)友 wangk ; free2000fly  修改

            MSDN中關(guān)于信號量的解釋如下:
            {注:在某些書籍中 Semaphore Objects 翻譯為“信標(biāo)”,有些則翻譯為“信號量”,本文均稱為“信號量”}
            信號量(Semaphore Objects)
            A semaphore object is a synchronization object that maintains a count between zero and a specified maximum value. The count is decremented each time a thread completes a wait for the semaphore object and incremented each time a thread releases the semaphore.
            信號量是維護0到指定最大值之間的計數(shù)器的同步對象.當(dāng)線程完成一次信號量的等待時,計數(shù)器自減1,當(dāng)線程釋放信號量對象時,計數(shù)器自增1
            {信號量用于對資源進行計數(shù),它包括了
            1、帶符號的數(shù)值 保存最大的資源數(shù)
            2、帶符號的數(shù)值 保存當(dāng)前可用的資源數(shù)
            }

            When the count reaches zero, no more threads can successfully wait for the semaphore object state to become signaled.  The state of a semaphore is set to signaled when its count is greater than zero, and nonsignaled when its count is zero.
            當(dāng)計數(shù)器為0時,不會再有其它線程等待信號量為受信狀態(tài).計數(shù)器的值大于0時信號量對象受信,等于0時便不會受信.

            The semaphore object is useful in controlling a shared resource that can support a limited number of users.
            信號量對象用于支持有限用戶的共享資源控制

            It acts as a gate that limits the number of threads sharing the resource to a specified maximum number.
            它可以作為限制線程共享資源最大值的一種方法

            For example, an application might place a limit on the number of windows that it creates. 
            It uses a semaphore with a maximum count equal to the window limit, decrementing the count whenever a window is created and incrementing it whenever a window is closed. The application specifies the semaphore object in call to one of the wait functions before each window is created. When the count is zero — indicating that the window limit has been reached — the wait function blocks execution of the window-creation code.

            例如,一個應(yīng)用程序限制它所創(chuàng)建的窗體.把信號量的最大值設(shè)置為窗體個數(shù)的最大值, 當(dāng)窗體創(chuàng)建時,信號量的值減少,當(dāng)窗體關(guān)閉時,信號量的值增加.應(yīng)用程序在窗體創(chuàng)建時調(diào)用 Wait 系列API函數(shù).當(dāng)計數(shù)為0時,證明窗體個數(shù)已經(jīng)達到極限, wait 系列函數(shù)會阻塞創(chuàng)建窗體的代碼

            A thread uses the CreateSemaphore or CreateSemaphoreEx function to create a semaphore object. The creating thread specifies the initial count and the maximum value of the count for the object. The initial count must be neither less than zero nor greater than the maximum value.
            The creating thread can also specify a name for the semaphore object. Threads in other processes can open a handle to an existing semaphore object by specifying its name in a call to the OpenSemaphore function.

            線程可以使用 CreateSemaphore 或 CreateSemaphoreEx 創(chuàng)建信號量對象
            創(chuàng)建信號量的線程需要指出信號量的初始值和最大值
            初始值必須大于0且不大于所指定的最大值
            創(chuàng)建的線程同樣可以為信號量指定一個名字
            其它進程的線程可以通過這個名字,然后調(diào)用 OpenSemaphore() 來打開已經(jīng)存在的信號量對象

            For additional information about names for mutex, event, semaphore, and timer objects, see Interprocess Synchronization.
            If more than one thread is waiting on a semaphore, a waiting thread is selected. Do not assume a first-in, first-out (FIFO) order. External events such as kernel-mode APCs can change the wait order.

            如果多于一條線程在等待信號量對象,wait 返回的那條線程并不會遵循先進先出(FIFO)的順序,因為內(nèi)核模式可能改變它的順序。

            Each time one of the wait functions returns because the state of a semaphore was set to signaled, the count of the semaphore is decreased by one. The ReleaseSemaphore function increases a semaphore's count by a specified amount. The count can never be less than zero or greater than the maximum value.
            每次信號量受信,wait 系列函數(shù)就會返回, 同時計數(shù)會減1,
            ReleaseSemaphore() 函數(shù)用來增加信號量的計數(shù)值,這個值永遠(yuǎn)大于等于0且小于等于指定的最大值

            The initial count of a semaphore is typically set to the maximum value. The count is then decremented from that level as the protected resource is consumed. Alternatively, you can create a semaphore with an initial count of zero to block access to the protected resource while the application is
            being initialized. After initialization, you can use ReleaseSemaphore to increment the count to the maximum value.

            信號量的初始值通常被設(shè)置為最大值,當(dāng)被保護的資源被消耗時,其計數(shù)值會被減少。或者,你可以在創(chuàng)建信號值時把初始值指定為 0, 應(yīng)用程序初始化時,保護資源會阻塞, 初始化后,你可以調(diào)用 ReleaseSemaphore  來增加信號量的計數(shù)值

            A thread that owns a mutex object can wait repeatedly for the same mutex object to become signaled without its execution becoming blocked. A thread that waits repeatedly for the same semaphore object, however, decrements the semaphore's count each time a wait operation is completed; the thread is blocked when the count gets to zero. Similarly, only the thread that owns a mutex can successfully call the ReleaseMutex function, though any thread can use ReleaseSemaphore to increase the count of a semaphore object.

            一條線程可以反復(fù)地等待 mutex 對象,在它受信前,不會去執(zhí)行阻塞的代碼塊
            同樣,一條線程也可以反復(fù)地等待 信號量 對象,
            然而,在 wait 操作完成后,信號量的計數(shù)會減1,
            當(dāng)為0時,線程會阻塞
            同樣地,只有當(dāng)前擁有該mutex 的線程可以調(diào)用 ReleaseMutex 函數(shù)
            但可以在任何線程通過調(diào)用 ReleaseSemaphore 函數(shù)來增加 信號量的計數(shù)

            A thread can decrement a semaphore's count more than once by repeatedly specifying the same semaphore object in calls to any of the wait functions. However, calling one of the multiple-object wait functions with an array that contains multiple handles of the same semaphore does not result in multiple decrements.

            線程可以通過反復(fù)等待一個信號量對象來讓信號量的計數(shù)自減
            但是,如果調(diào)用 multiple-object wait 系列函數(shù),等待的數(shù)組里指定為多個相同的信號量,這時并不會導(dǎo)致信號量的計數(shù)值多次自減。

            相關(guān)WIN-API函數(shù):
            CreateSemaphore()
            OpenSemaphore()
            ReleaseSemaphore()
            WaitForSingleObject()
            CloseHandle()

            本文內(nèi)容摘自MSDN,有些地方翻譯得不好
            歡迎交流,指正
            聯(lián)系QQ:    859934020(經(jīng)常隱身)
            聯(lián)系E-mail:  kredraw@21cn.com

            Feedback

            # re: Windows多線程之信號量(Semaphore) {不斷補充中...最后更新08.08.26}[未登錄]  回復(fù)  更多評論   

            2008-08-27 10:17 by 陳梓瀚(vczh)
            這種玩意兒還有Mutex , WaitableTimer等等,可以用于進程間通訊。CriticalSection只能夠進程內(nèi)部通訊。

            # re: Windows多線程之信號量(Semaphore) {不斷補充中...最后更新08.08.26}  回復(fù)  更多評論   

            2008-08-27 10:30 by free2000fly
            "同樣地,只有創(chuàng)建 mutex 的線程可以調(diào)用 ReleaseMutex 函數(shù) "
            應(yīng)為
            "同樣地,只有當(dāng)前擁有 mutex 的線程可以成功地調(diào)用 ReleaseMutex 函數(shù) "
            謝謝你的翻譯

            # re: Windows多線程之信號量(Semaphore) {不斷補充中...最后更新08.08.27}  回復(fù)  更多評論   

            2008-08-27 17:27 by 沒畫完的畫
            @陳梓瀚(vczh)
            嗯,有空一定補上去

            # re: Windows多線程之信號量(Semaphore) {不斷補充中...最后更新08.08.27}  回復(fù)  更多評論   

            2008-08-27 17:28 by 沒畫完的畫
            @free2000fly
            Thx
            伊人久久综合成人网| 久久久久国产一区二区| 久久人妻AV中文字幕| 色欲综合久久躁天天躁| 欧美成人免费观看久久| 久久久受www免费人成| 要久久爱在线免费观看| 中文字幕人妻色偷偷久久| 精品国产乱码久久久久软件| 亚洲人成伊人成综合网久久久 | 理论片午午伦夜理片久久 | 99久久精品免费观看国产| 国产成人99久久亚洲综合精品| 精品久久久无码中文字幕天天| 国产亚州精品女人久久久久久 | 欧美激情精品久久久久久久九九九| 久久久久久亚洲精品无码| 中文字幕人妻色偷偷久久| 久久婷婷综合中文字幕| 中文精品久久久久人妻不卡| 国产精品久久网| 久久亚洲日韩看片无码| 国产成人久久久精品二区三区 | 久久毛片免费看一区二区三区| 亚洲中文字幕久久精品无码APP| 久久中文字幕一区二区| 天天爽天天狠久久久综合麻豆| 久久夜色撩人精品国产| 久久免费国产精品一区二区| 久久精品国产久精国产思思| 一本一道久久综合狠狠老 | 久久久久久久久无码精品亚洲日韩| 91秦先生久久久久久久| 青青青国产精品国产精品久久久久| 老色鬼久久亚洲AV综合| 国产午夜免费高清久久影院| 激情伊人五月天久久综合| 99re久久精品国产首页2020| 久久久久久午夜成人影院| 亚洲精品无码久久久久去q| 国产V综合V亚洲欧美久久|