• <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>
            隨筆-161  評論-223  文章-30  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字節長度。
               
            bool類型完全特化
               由于大小為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;
            posted on 2012-06-08 00:19 春秋十二月 閱讀(4578) 評論(1)  編輯 收藏 引用 所屬分類: C/C++
            精品久久人人爽天天玩人人妻| 久久亚洲精品国产亚洲老地址| 久久国产免费直播| 久久精品国产日本波多野结衣| 99久久做夜夜爱天天做精品| 亚洲精品无码久久久久sm| 日韩人妻无码一区二区三区久久| 久久不射电影网| 蜜臀久久99精品久久久久久| 亚洲AV日韩精品久久久久久久| 久久99国产精品久久| 久久受www免费人成_看片中文| 久久婷婷五月综合97色| 欧美麻豆久久久久久中文| 久久亚洲精品国产精品| 久久有码中文字幕| 精品午夜久久福利大片| 亚洲va国产va天堂va久久| 久久人妻少妇嫩草AV无码蜜桃| 亚洲精品乱码久久久久久久久久久久 | 香港aa三级久久三级老师2021国产三级精品三级在 | 久久成人18免费网站| 中文字幕无码免费久久| 久久影院久久香蕉国产线看观看| 久久99精品久久久久久| 久久99精品久久久久子伦| 一级a性色生活片久久无| www.久久精品| 无码人妻久久一区二区三区| 国产成人香蕉久久久久| 国产人久久人人人人爽| 亚洲成色WWW久久网站| 2021国内久久精品| 日日狠狠久久偷偷色综合0| 久久99国产精品成人欧美| 国产91久久精品一区二区| 久久久久久国产精品免费无码| 无码人妻精品一区二区三区久久 | 久久久av波多野一区二区| 麻豆精品久久久久久久99蜜桃| 性做久久久久久久久|