• <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>

            loop_in_codes

            低調做技術__歡迎移步我的獨立博客 codemaro.com 微博 kevinlynx

            IOCP與線程

            author : Kevin Lynx

             

            什么是完成包?

            完成包,即IO Completion Packet,是指異步IO操作完畢后OS提交給應用層的通知包。IOCP維護了一個IO操作結果隊列,里面
            保存著各種完成包。應用層調用GQCS(也就是GetQueueCompletionStatus)函數獲取這些完成包。

            最大并發線程數

            在一個典型的IOCP程序里,會有一些線程調用GQCS去獲取IO操作結果。最大并發線程數指定在同一時刻處理完成包的線程數目。
            該參數在調用CreateIoCompletionPort時由NumberOfConcurrentThreads指定。

            工作者線程

            工作者線程一般指的就是調用GQCS函數的線程。要注意的是,工作者線程數和最大并發線程數并不是同一回事(見下文)。工作者
            線程由應用層顯示創建(_beginthreadex 之類)。工作者線程通常是一個循環,會不斷地GQCS到完成包,然后處理完成包。

            調度過程

            工作者線程以是否阻塞分為兩種狀態:運行狀態和等待狀態。當線程做一些阻塞操作時(線程同步,甚至GQCS空的完成隊列),線程
            處于等待狀態;否則,線程處于運行狀態。

            另一方面,OS會始終保持某一時刻處于運行狀態的線程數小于最大并發線程數。每一個調用GQCS函數的線程OS實際上都會進行記錄,
            當完成隊列里有完成包時,OS會首先檢查當前處于運行狀態的工作線程數是否小于最大并發線程數,如果小于,OS會按照LIFO的順
            序讓某個工作者線程從GQCS返回(此工作者線程轉換為運行狀態)。如何決定這個LIFO?這是簡單地通過調用GQCS函數的順序決定的。

            從這里可以看出,這里涉及到線程喚醒和睡眠的操作。如果兩個線程被放置于同一個CPU上,就會有線程切換的開銷。因此,為了消
            除這個開銷,最大并發線程數被建議為設置成CPU數量。

            從以上調度過程還可以看出,如果某個處于運行狀態的工作者線程在處理完成包時阻塞了(例如線程同步、其他IO操作),那么就有
            CPU資源處于空閑狀態。因此,我們也看到很多文檔里建議,工作者線程數為(CPU數*2+2)。

            在一個等待線程轉換到運行狀態時,有可能會出現短暫的時間運行線程數超過最大并發線程數,這個時候OS會迅速地讓這個新轉換
            的線程阻塞,從而減少這個數量。(關于這個觀點,MSDN上只說:by not allowing any new active threads,卻沒說明not allowing
            what)

            調度原理

            這個知道了其實沒什么意義,都是內核做的事,大致上都是操作線程control block,直接摘錄<Inside IO Completion Ports>:

            The list of threads hangs off the queue object. A thread's control block data structure has a pointer in it that
            references the queue object of a queue that it is associated with; if the pointer is NULL then the thread is not
            associated with a queue.

            So how does NT keep track of threads that become inactive because they block on something other than the completion
            port" The answer lies in the queue pointer in a thread's control block. The scheduler routines that are executed
            in response to a thread blocking (KeWaitForSingleObject, KeDelayExecutionThread, etc.) check the thread's queue
            pointer and if its not NULL they will call KiActivateWaiterQueue, a queue-related function. KiActivateWaiterQueue
            decrements the count of active threads associated with the queue, and if the result is less than the maximum and
            there is at least one completion packet in the queue then the thread at the front of the queue's thread list is
            woken and given the oldest packet. Conversely, whenever a thread that is associated with a queue wakes up after
            blocking the scheduler executes the function KiUnwaitThread, which increments the queue's active count.

            參考資料

            <Inside I/O Completion Ports>:
            http://technet.microsoft.com/en-us/sysinternals/bb963891.aspx
            <I/O Completion Ports>:
            http://msdn.microsoft.com/en-us/library/aa365198(VS.85).aspx
            <INFO: Design Issues When Using IOCP in a Winsock Server>:
            http://support.microsoft.com/kb/192800/en-us/

            posted on 2008-06-23 17:32 Kevin Lynx 閱讀(4842) 評論(3)  編輯 收藏 引用 所屬分類: network

            評論

            # re: IOCP與線程 2008-06-23 23:45 飯中淹

            @Kevin Lynx
            寫iocp應用的時候,我覺得最好能夠實現工作線程就地組包,然后在組包之后,在工作線程內就地處理,這樣是對iocp和cpu資源的最佳化利用。

            還有,多謝你的幫助,我才糾正了對iocp的錯誤理解。

              回復  更多評論   

            # re: IOCP與線程 2010-06-04 11:02 我是豬

            @飯中淹
            錯了 這樣會浪費效能  回復  更多評論   

            # re: IOCP與線程 2010-06-29 09:55 zhengxf

            你好,我也認為寫iocp應用的時候, 應該工作線程就地組包,然后在組包之后,在工作線程內就地處理, 不知道"飯中淹" 老兄有何高見,能分享一下嗎.
              回復  更多評論   

            久久综合狠狠色综合伊人| 婷婷伊人久久大香线蕉AV| 99久久久久| 久久综合九色欧美综合狠狠| 伊人精品久久久久7777| 亚洲国产精品无码久久| 99久久国产免费福利| 欧美色综合久久久久久| 成人妇女免费播放久久久| 久久综合成人网| 99久久精品国产高清一区二区| 99久久免费只有精品国产| 中文字幕日本人妻久久久免费 | 国产精品九九久久免费视频| 亚洲人AV永久一区二区三区久久| 国内精品久久久久| 精品多毛少妇人妻AV免费久久| 久久综合综合久久狠狠狠97色88| 久久中文字幕人妻熟av女| 久久久艹| 国产精品日韩欧美久久综合| 国产成人精品免费久久久久| 99久久精品免费看国产一区二区三区| 99久久精品国产综合一区| 精品国产VA久久久久久久冰| 精品久久久无码人妻中文字幕| 一级做a爰片久久毛片免费陪| 亚洲精品国产成人99久久| 久久精品国产精品亚洲毛片| 7777精品久久久大香线蕉 | 狠狠色综合网站久久久久久久| 亚洲AV日韩AV永久无码久久| 久久久久av无码免费网| 久久久无码精品亚洲日韩蜜臀浪潮 | 中文字幕久久亚洲一区| 亚洲欧洲精品成人久久奇米网| 久久精品国产一区二区| 国产亚洲美女精品久久久| 精品久久久久国产免费| 久久97久久97精品免视看秋霞| 91久久九九无码成人网站|