• <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>
            面對現(xiàn)實,超越自己
            逆水行舟,不進(jìn)則退
            posts - 269,comments - 32,trackbacks - 0

            author : Kevin Lynx

             

            什么是完成包?

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

            最大并發(fā)線程數(shù)

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

            工作者線程

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

            調(diào)度過程

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

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

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

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

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

            調(diào)度原理

            這個知道了其實沒什么意義,都是內(nèi)核做的事,大致上都是操作線程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/

            本文轉(zhuǎn)自:http://m.shnenglu.com/kevinlynx/archive/2008/06/23/54390.html

            posted on 2012-09-20 13:12 王海光 閱讀(500) 評論(0)  編輯 收藏 引用 所屬分類: C++
            久久亚洲精品成人AV| 亚洲а∨天堂久久精品9966| 久久夜色精品国产网站| 国产三级久久久精品麻豆三级| 99久久久国产精品免费无卡顿| 精品无码久久久久久久久久| 久久久久高潮综合影院| 亚洲午夜久久久精品影院 | 国产一级持黄大片99久久| 91久久福利国产成人精品| 亚洲第一永久AV网站久久精品男人的天堂AV | 婷婷久久五月天| 91久久精品视频| 久久精品国产网红主播| 精品国产乱码久久久久久呢| 国产成人99久久亚洲综合精品| 人妻少妇久久中文字幕| 久久亚洲AV无码西西人体| 国产精品久久久久无码av| 一本色道久久综合亚洲精品| 久久久久亚洲AV成人网人人网站 | 亚洲狠狠婷婷综合久久蜜芽| AAA级久久久精品无码区| 无码人妻少妇久久中文字幕蜜桃| 国产真实乱对白精彩久久| 97r久久精品国产99国产精| 色偷偷偷久久伊人大杳蕉| 亚洲性久久久影院| 久久中文精品无码中文字幕| 久久精品国产久精国产| 国产∨亚洲V天堂无码久久久| 精品国产青草久久久久福利| 久久国产劲爆AV内射—百度| 亚洲乱码日产精品a级毛片久久| 久久99精品国产麻豆蜜芽| 精品久久久久久无码国产| 久久久久亚洲精品无码网址| 久久综合九色综合久99| 日韩欧美亚洲综合久久| 国产精品乱码久久久久久软件| 国产毛片欧美毛片久久久|