• <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>
            隨筆-167  評論-8  文章-0  trackbacks-0
               gcc從4.1.2版本開始提供了__sync_*系列的內置API,用于加減和邏輯運算,可以對1,2,4,8字節長度的數值或指針類型進行原子操作。為方便使用,個人對這些API作了簡單的封裝,主模板類如下:   
             1template<typename T>
             2class atomic_base 
             3{
             4public:
             5    typedef T int_type;
             6    typedef atomic_base<T> self_type;
             7
             8public:
             9    atomic_base(int_type val = int_type())
            10    : val_(val)
            11    {
            12    }

            13
            14    ~atomic_base()
            15    {
            16        BOOST_STATIC_ASSERT(sizeof(int_type)==1||sizeof(int_type)==2||sizeof(int_type)==4||sizeof(int_type)==8);
            17    }

            18
            19    atomic_base(const self_type& other)
            20    {
            21        int_type val = other;
            22        __sync_lock_test_and_set(&val_, val);
            23    }

            24
            25    self_type& operator = (const self_type& other)
            26    {
            27        if (this!=&other)
            28        {
            29            int_type val = other;
            30            __sync_lock_test_and_set(&val_, val);
            31        }

            32        return *this;
            33    }

            34    
            35    operator int_type() const
            36    {
            37        return __sync_val_compare_and_swap(const_cast<volatile int_type*>(&val_),0,0);
            38    }

            39    
            40    self_type operator++(int)
            41    {
            42        return __sync_fetch_and_add(&val_,1);
            43    }

            44
            45    self_type& operator++()
            46    {
            47        __sync_add_and_fetch(&val_,1);
            48        return *this;
            49    }

            50
            51    self_type operator--(int)
            52    {
            53        return __sync_fetch_and_sub(&val_,1);
            54    }

            55
            56    self_type& operator--()
            57    {
            58        __sync_sub_and_fetch(&val_,1);
            59        return *this;
            60    }

            61
            62    self_type& operator+=(int_type val)
            63    {
            64        __sync_add_and_fetch(&val_,val);
            65        return *this;
            66    }

            67
            68    self_type& operator-=(int_type val)
            69    {
            70        __sync_sub_and_fetch(&val_,val);
            71        return *this;
            72    }

            73
            74    self_type& operator&=(int_type val)
            75    
            76        __sync_and_and_fetch(&val_, val); 
            77        return *this;
            78    }

            79
            80    self_type& operator|=(int_type val)
            81    {
            82        __sync_or_and_fetch(&val_,val); 
            83        return *this;
            84    }

            85    
            86    self_type& operator^=(int_type val)
            87    {
            88        __sync_xor_and_fetch(&val_,val); 
            89        return *this;
            90    }

            91
            92private:
            93    T val_;
            94}
            ;
               上面析構函數中,使用了BOOST_STATIC_ASSERT宏來作編譯期約束,確保類型T大小為1,2,4,8字節長度。由于提供了T到atomic_base的隱式轉換和轉型操作,因此沒有必要再定義operato +,operator - ,opeator ^,operator |,operator &運算符成員或友元重載操作。由于主模板定義了operator+=,operator-=,operator&=,operator|=,operator^=運算符重載操作,但當T為指針類型時,指針的加減和邏輯運算沒有意義,但對于指針加減一個數值是有意義的,因此前兩者操作保留但需要重定義,但沒有后三者操作,需要特化實現,其模板類如下:
             1template<typename T>
             2class atomic_base<T*> 
             3{
             4public:
             5    typedef T* pointer_type;
             6    typedef atomic_base<T*> self_type;
             7
             8public:
             9    atomic_base(pointer_type val = NULL)
            10        : val_(val)
            11    {
            12    }

            13
            14    atomic_base(const self_type& other)
            15    {
            16        pointer_type val = other;
            17        __sync_lock_test_and_set(&val_, val);
            18    }

            19
            20    ~atomic_base()
            21    {
            22        BOOST_STATIC_ASSERT(sizeof(pointer_type)==1||sizeof(pointer_type)==2||sizeof(pointer_type)==4||sizeof(pointer_type)==8);
            23    }

            24
            25    self_type& operator=(const self_type& other)
            26    {
            27        if (this!=&other)
            28        {
            29            pointer_type val = other;
            30            __sync_lock_test_and_set(&val_, val);
            31        }

            32        return *this;
            33    }

            34
            35    operator pointer_type() const
            36    {
            37        return __sync_val_compare_and_swap(const_cast<volatile pointer_type*>(&val_),0,0);
            38    }

            39
            40    self_type operator++(int)
            41    {
            42        return __sync_fetch_and_add(&val_,1);
            43    }

            44
            45    self_type& operator++()
            46    {
            47        __sync_add_and_fetch(&val_,1);
            48        return *this;
            49    }

            50
            51    self_type operator--(int)
            52    {
            53        return __sync_fetch_and_sub(&val_,1);
            54    }

            55
            56    self_type& operator--()
            57    {
            58        __sync_sub_and_fetch(&val_,1);
            59        return *this;
            60    }

            61
            62    self_type& operator+=(std::ptrdiff_t d)
            63    {
            64        __sync_add_and_fetch(&val_,d);
            65        return *this;
            66    }

            67
            68    self_type& operator-=(std::ptrdiff_t d)
            69    {
            70        __sync_sub_and_fetch(&val_,d);
            71        return *this;
            72    }

            73
            74private:
            75    T* val_;
            76}
            ;
              同理,上面析構函數中,使用了BOOST_STATIC_ASSERT宏來作編譯期約束,確保類型T的指針大小為1,2,4,8字節長度。在這里,要注意一個特例,大小為1字節的類型包括bool布爾類型,它的取值只有true或false兩種可能,不存在加減運算操作,也沒有意義,但邏輯運算是有意義的,因此需要特化實現,其模板類如下:
             1template<>
             2class atomic_base<bool>
             3{
             4public:
             5    typedef atomic_base<bool> self_type;
             6public:
             7    atomic_base(bool val = false)
             8        : val_(val)
             9    {
            10    }

            11    
            12    atomic_base(const self_type& other)
            13    {
            14        __sync_lock_test_and_set(&val_, other.val_);
            15    }

            16    
            17    self_type& operator=(const self_type& other)
            18    {
            19        if (this!=&other)
            20            __sync_lock_test_and_set(&val_, other.val_);
            21        return *this;
            22    }

            23    
            24    operator bool() const
            25    {
            26        return __sync_val_compare_and_swap(const_cast<volatile bool*>(&val_),0,0);
            27    }

            28
            29    self_type& operator&=(bool val)
            30    
            31        __sync_and_and_fetch(&val_, val); 
            32        return *this;
            33    }

            34
            35    self_type& operator|=(bool val)
            36    {
            37        __sync_or_and_fetch(&val_,val); 
            38        return *this;
            39    }

            40
            41    self_type& operator^=(bool val)
            42    {
            43        __sync_xor_and_fetch(&val_,val); 
            44        return *this;
            45    }

            46
            47private:
            48    bool val_;
            49}
            ;
               針對常見的基本數值類型,定義了其類型別名,如下:
             1typedef atomic_base<bool>                     atomic_bool;
             2typedef atomic_base<signed char>           atomic_char;
             3typedef atomic_base<unsigned char>        atomic_uchar;
             4typedef atomic_base<short>                    atomic_short;
             5typedef atomic_base<unsigned short>       atomic_ushort;
             6typedef atomic_base<int>                       atomic_int;
             7typedef atomic_base<unsigned int>          atomic_uint;
             8typedef atomic_base<long>                     atomic_long;
             9typedef atomic_base<unsigned long>        atomic_ulong;
            10typedef atomic_base<long long>              atomic_llong;
            11typedef atomic_base<unsigned long long>  atomic_ullong;
               幸運的是,從gcc 4.4起,標準庫就提供了atomic C++類,尤其是4.6(支持C++0x標準),作了很好的封裝,相關頭文件是atomic_base.h, atomic_0.h, atomic_2.h, atomic.h。
            posted on 2011-09-13 18:58 老馬驛站 閱讀(982) 評論(0)  編輯 收藏 引用 所屬分類: linux
            久久久精品久久久久久 | 国产精品一区二区久久精品| 亚洲国产另类久久久精品小说| 精品久久久久久国产牛牛app| 国内精品伊人久久久久av一坑| 国产欧美久久久精品影院| 亚洲精品无码专区久久同性男| 久久久青草青青国产亚洲免观| 91麻精品国产91久久久久| 免费观看久久精彩视频| 精品久久久久久久久中文字幕| 国内精品久久久久久野外| 久久电影网2021| 久久精品无码一区二区三区日韩| 国产69精品久久久久9999| 久久精品国产99久久香蕉| 亚洲Av无码国产情品久久| 久久亚洲中文字幕精品一区| 亚洲AV日韩AV永久无码久久| 天天爽天天狠久久久综合麻豆| 国产成年无码久久久久毛片| 久久久久夜夜夜精品国产| 久久99精品九九九久久婷婷| 日本国产精品久久| 少妇高潮惨叫久久久久久| 久久美女网站免费| 中文精品久久久久人妻| 欧美黑人又粗又大久久久| 国产亚洲精午夜久久久久久| 亚洲日本va午夜中文字幕久久 | 亚洲а∨天堂久久精品| 久久精品国产2020| 国内精品久久久久久久久| 久久久久久久精品妇女99| 狠狠干狠狠久久| 99久久香蕉国产线看观香| 九九99精品久久久久久| 亚洲日韩欧美一区久久久久我 | 久久天天躁夜夜躁狠狠| 无遮挡粉嫩小泬久久久久久久| 欧洲人妻丰满av无码久久不卡|