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

            飯中淹的避難所~~~~~

            偶爾來(lái)避難的地方~

              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              94 隨筆 :: 0 文章 :: 257 評(píng)論 :: 0 Trackbacks
            經(jīng)由FCC同學(xué)提醒,我才發(fā)現(xiàn),之前的兩篇用volatile的無(wú)鎖通信,確實(shí)是敗在了優(yōu)化上了。編譯器和CPU硬件會(huì)無(wú)情的擊毀我們認(rèn)為的執(zhí)行順序。

            這篇(0)就是否定掉前兩篇,然后在未來(lái)的(3)和(4)里面,將用cas等原語(yǔ)來(lái)重新設(shè)計(jì)這個(gè)無(wú)鎖的通信。

            關(guān)于編譯器的優(yōu)化,我猜是為了CPU的U/V流水線來(lái)做的。它讓狀態(tài)的修改操作,有可能在操作之前就發(fā)生了,從而直接顛覆了整個(gè)方法。

            另外shbooom所說(shuō)的,兩個(gè)原子操作的問(wèn)題。可能是少打了一個(gè)不字。

            我說(shuō)的原子操作,是在最終操作上來(lái)理解的,或者稱(chēng)為內(nèi)部因果關(guān)聯(lián)上。就是說(shuō),對(duì)狀態(tài)賦值前的取地址操作,都不算作賦值操作的一部分。同樣的對(duì)狀態(tài)進(jìn)行檢查的時(shí)候的取地址操作,也不算作檢查的一部分。因?yàn)樗麄兓ゲ挥绊憽W罱K影響檢查狀態(tài)的,只有寫(xiě)內(nèi)存位置的那一個(gè)操作。在這之間出現(xiàn)的執(zhí)行穿插,最多會(huì)導(dǎo)致本次檢查無(wú)法命中,但下次檢查就一定會(huì)命中。



            [Barrier]

            在多篇文章中,我找到了解決辦法。可以通過(guò)Barrier結(jié)束掉之前的所有讀寫(xiě)操作,從而讓設(shè)置狀態(tài)這個(gè)操作的寫(xiě)操作不會(huì)被優(yōu)化到實(shí)際任務(wù)操作的中間或者前面去。

            在VC中,有_ReadBarrier _WriteBarrier 和 _ReadWriteBarrier的特殊指令,可以將之前的所有對(duì)內(nèi)存的讀和寫(xiě)或者讀寫(xiě)的優(yōu)化限制到Barrier之前,從而不會(huì)把后面的操作優(yōu)化到前面去。

            在其他的編譯器中,應(yīng)該也有相應(yīng)的東西來(lái)保障執(zhí)行順序。

            不過(guò)我還沒(méi)測(cè)試過(guò)這個(gè)對(duì)性能帶來(lái)的影響。希望不要太大。
            posted on 2010-05-06 16:59 飯中淹 閱讀(2096) 評(píng)論(9)  編輯 收藏 引用 所屬分類(lèi): 數(shù)據(jù)算法分析

            評(píng)論

            # re: 無(wú)鎖多線程通信(0):回到起點(diǎn) 2010-05-06 18:01 shbooom
            if(locked==false) { locked=true; doSomething();}這不是兩個(gè)原子操作?如何防止兩步之間沒(méi)有人插入?
              回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖多線程通信(0):回到起點(diǎn) 2010-05-06 18:36 飯中淹
            @shbooom
            不是locked,是dosomething.
            線程A
            if(doSomething==true) { DoSomething(); doSomething=false;}

            線程B
            if(doSomething==false) { DoOtherthing(); doSomething=true;}

            在A線程DoSomething()時(shí),B線程進(jìn)不去判斷,所以不會(huì)用DoOtherthing()來(lái)干擾。

            或者就按照你的locked
            修改成這樣
            線程A
            if(locked==false) { doSomething(); locked=true;}
            線程B
            if(locked) { doOtherthing(); locked = false;}

            在A的花括號(hào)內(nèi),B的doOtherthing不會(huì)影響A的doSomething。





              回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖多線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-05-06 18:58 cpm
            Intel Architectures Software Developer's Manual Volume 3A System Programming Guide Chapter 7 Section 2
            7.2.2 Memory Ordering in P6 and More Recent Processor Families
            The Intel Core 2 Duo, Intel Atom, Intel Core Duo, Pentium 4, and P6 family proces-
            sors also use a processor-ordered memory-ordering model that can be further
            defined as "write ordered with store-buffer forwarding." This model can be character-
            ized as follows.
            In a single-processor system for memory regions defined as write-back cacheable,
            the following ordering principles apply (Note the memory-ordering principles for
            single-processor and multiple-processor systems are written from the perspective of
            software executing on the processor, where the term "processor" refers to a logical
            processor. For example, a physical processor supporting multiple cores and/or
            HyperThreading Technology is treated as a multi-processor systems.):
            1 Reads are not reordered with other reads.
            2 Writes are not reordered with older reads.
            3 Writes to memory are not reordered with other writes, with the exception of
            writes executed with the CLFLUSH instruction and streaming stores (writes)
            executed with the non-temporal move instructions (MOVNTI, MOVNTQ,
            MOVNTDQ, MOVNTPS, and MOVNTPD).
            4 Reads may be reordered with older writes to different locations but not with older
            writes to the same location.
            5 Reads or writes cannot be reordered with I/O instructions, locked instructions, or
            serializing instructions.
            6 Reads cannot pass LFENCE and MFENCE instructions.
            7 Writes cannot pass SFENCE and MFENCE instructions.
            In a multiple-processor system, the following ordering principles apply:
            1 Individual processors use the same ordering principles as in a single-processor
            system.
            2 Writes by a single processor are observed in the same order by all processors.
            3 Writes from an individual processor are NOT ordered with respect to the writes
            from other processors.
            4 Memory ordering obeys causality (memory ordering respects transitive
            visibility).
            5 Writes to the same location have a total order.
            6 Locked instructions have a total order.

            其實(shí)只有寫(xiě)操作和其后的讀操作(不同的地址)才會(huì)亂序執(zhí)行  回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖多線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-05-06 19:07 cpm
            看了你之前的源代碼,當(dāng)線程A需要放入數(shù)據(jù)時(shí)B線程一直在使用數(shù)據(jù)的話,線程A會(huì)占100%的CPU,系統(tǒng)里可不光A,B兩個(gè)線程,如果使用系統(tǒng)提供的臨界區(qū),線程A會(huì)陷入內(nèi)核然后釋放出CPU控制權(quán),等線程B處理完會(huì)將A喚醒,提高了系統(tǒng)的利用率。  回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖多線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-05-06 19:22 飯中淹
            @cpm
            代碼中的賦值是在一起的,而且是對(duì)不同的變量,所以他們可能會(huì)被亂序執(zhí)行。這樣另一個(gè)線程獲取到通過(guò)狀態(tài)后,可能這時(shí)候其他的寫(xiě)操作還沒(méi)有執(zhí)行完。就會(huì)出問(wèn)題。

            所以在修改狀態(tài)前,要用一個(gè)寫(xiě)barrier來(lái)過(guò)濾掉其他寫(xiě)操作的reorder,從而實(shí)現(xiàn)修改狀態(tài)時(shí),所有寫(xiě)操作都完成。


            另外,關(guān)于例程的代碼,只是做分析用。實(shí)際使用時(shí),還是得用一些手段來(lái)讓系統(tǒng)對(duì)線程進(jìn)行調(diào)度。這個(gè)是最基本的常識(shí)。
              回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-05-07 15:24 cpm
            @飯中淹
            3 Writes to memory are not reordered with other writes, with the exception ...
            所以寫(xiě)操作和寫(xiě)操作之間不會(huì)亂序(例外情況這里不會(huì)遇到)
            同時(shí)
            2 Writes by a single processor are observed in the same order by all processors.
            因此,一個(gè)線程的寫(xiě)操作也是被所有線程依次觀察到的(例如一個(gè)線程先寫(xiě)了a,后寫(xiě)了b,那么所有線程觀察的結(jié)果都是a先被寫(xiě),b后被寫(xiě))
            所以這種情況下不需要內(nèi)存屏障,只要變量加了volatile保證編譯器不優(yōu)化就沒(méi)有問(wèn)題


            另外,你的代碼之所以無(wú)鎖能夠運(yùn)行,是因?yàn)橥ǔP枰嗑€程鎖的那些線程地位是對(duì)等的,而在你的例子里,其實(shí)是把一個(gè)單線程問(wèn)題披上了多線程的外套。A線程只會(huì)把iState 由false改為true,而B(niǎo)線程只會(huì)把iState 由true改為false。既然這樣,為何不讓A線程檢查完直接處理呢?
            而真正需要多線程同步的問(wèn)題(即不能簡(jiǎn)化為單線程的問(wèn)題),鎖是不可避免的。就算是lock-free編程,也是使用了硬件鎖。  回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-05-07 16:57 飯中淹
            @cpm
            在這里,我說(shuō)的是個(gè)多線程通信問(wèn)題,它的單個(gè)任務(wù)是線性的。
            我認(rèn)為:

            所有的多線程問(wèn)題都可以細(xì)分為單個(gè)線性任務(wù)。

            不能細(xì)分為線性任務(wù)的問(wèn)題,是不存在的。

            鎖是不可避免的,就算是狀態(tài)也是一種鎖。

            這里的無(wú)鎖,就是不用系統(tǒng)提供的鎖。

            無(wú)論是什么多線程問(wèn)題,鎖都是保證線性任務(wù)線性執(zhí)行的。

            所以沒(méi)有你說(shuō)的不能簡(jiǎn)化為單線程的問(wèn)題。

              回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-05-07 17:03 飯中淹
            @cpm
            另外,我準(zhǔn)備用barrier來(lái)代替volatile,從而讓所有的讀寫(xiě)操作都能夠被優(yōu)化,并且,還能夠獲得及時(shí)的更新。
              回復(fù)  更多評(píng)論
              

            # re: 無(wú)鎖線程通信(0):回到起點(diǎn),又見(jiàn)曙光 2010-06-02 10:04 maybetrueness
            為什么不使用Intel的線程開(kāi)發(fā)包TBB呢?這個(gè)庫(kù)已經(jīng)被多個(gè)產(chǎn)品使用了,而不是實(shí)驗(yàn)性代碼。
            里面有模板 atomic<T> 來(lái)設(shè)定原子性變量,來(lái)代替volatile, 既保證正確性,又保證跨平臺(tái)。你們擔(dān)心的問(wèn)題,我相信Intel的工程師都已經(jīng)考慮到并且保證沒(méi)有問(wèn)題的。  回復(fù)  更多評(píng)論
              

            亚洲国产成人久久精品影视| 精品国产乱码久久久久软件| 狠狠色丁香久久婷婷综合_中| 久久精品无码专区免费东京热 | 久久久久人妻精品一区| 一级做a爰片久久毛片看看 | 欧美成人免费观看久久| 久久精品国产精品亚洲人人| 99久久无码一区人妻| 久久综合综合久久97色| 91精品国产91久久久久久蜜臀| 91精品国产综合久久久久久| 7777久久亚洲中文字幕| 亚洲乱亚洲乱淫久久| 久久97久久97精品免视看| 久久精品国产色蜜蜜麻豆| 色婷婷综合久久久久中文字幕| 久久久中文字幕日本| 性做久久久久久久久浪潮| 久久免费看黄a级毛片| 日产精品久久久久久久性色| 久久九九精品99国产精品| 久久亚洲综合色一区二区三区| 国内精品久久久久久久影视麻豆| 久久精品亚洲乱码伦伦中文| 伊人久久大香线蕉综合5g| 午夜精品久久久久久中宇| 精品久久久久久国产| 久久一区二区三区99| 久久久久AV综合网成人| 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 久久一本综合| 久久水蜜桃亚洲av无码精品麻豆| AV无码久久久久不卡网站下载| 97超级碰碰碰碰久久久久| 亚洲乱码日产精品a级毛片久久| 无码人妻精品一区二区三区久久久| 51久久夜色精品国产| 日韩人妻无码精品久久久不卡| 99久久99久久精品国产| 亚洲av伊人久久综合密臀性色 |