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

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

            偶爾來避難的地方~

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

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

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

            另外shbooom所說的,兩個原子操作的問題。可能是少打了一個不字。

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



            [Barrier]

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

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

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

            不過我還沒測試過這個對性能帶來的影響。希望不要太大。
            posted on 2010-05-06 16:59 飯中淹 閱讀(2101) 評論(9)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)算法分析

            評論

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

            # re: 無鎖多線程通信(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()時,B線程進(jìn)不去判斷,所以不會用DoOtherthing()來干擾。

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

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





              回復(fù)  更多評論
              

            # re: 無鎖多線程通信(0):回到起點(diǎ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í)只有寫操作和其后的讀操作(不同的地址)才會亂序執(zhí)行  回復(fù)  更多評論
              

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

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

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


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

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


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

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

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

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

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

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

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

            所以沒有你說的不能簡化為單線程的問題。

              回復(fù)  更多評論
              

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

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

            亚洲精品第一综合99久久| 热久久国产欧美一区二区精品| 久久人人爽人人爽人人爽| 久久婷婷色综合一区二区| 久久人人爽人人爽人人片AV不 | 久久国产精品成人片免费| 国产成人久久精品区一区二区| 久久亚洲国产午夜精品理论片| 久久久久亚洲精品男人的天堂| 久久精品国产亚洲av麻豆蜜芽| 久久噜噜电影你懂的| 久久精品国产亚洲AV影院| 亚洲国产成人久久综合碰碰动漫3d| 久久影视综合亚洲| 久久国产精品-国产精品| 久久乐国产综合亚洲精品| 久久99中文字幕久久| 国产色综合久久无码有码| 久久久久久一区国产精品| 99久久精品午夜一区二区| 亚洲国产精品无码久久九九| 伊人久久大香线蕉影院95| 人妻精品久久无码区| 三级三级久久三级久久| 久久国产精品免费一区| 2021精品国产综合久久| 亚洲综合日韩久久成人AV| 一本久久a久久精品综合香蕉| 91亚洲国产成人久久精品网址| 精品久久久久久无码专区不卡| 久久久黄色大片| 亚洲另类欧美综合久久图片区| 久久99精品久久久久久不卡| 日本久久久久久中文字幕| 国产精品福利一区二区久久| 国产亚洲欧美精品久久久| 久久精品中文闷骚内射| 久久er99热精品一区二区| 久久久久久国产精品无码超碰| 九九久久自然熟的香蕉图片| 久久久一本精品99久久精品66|