• <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>
            隨筆-159  評論-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 春秋十二月 閱讀(4570) 評論(1)  編輯 收藏 引用 所屬分類: C/C++
            无码人妻久久一区二区三区| 久久无码精品一区二区三区| 精品一久久香蕉国产线看播放| 婷婷国产天堂久久综合五月| 成人a毛片久久免费播放| 久久久久久人妻无码| 午夜精品久久久久久毛片| 亚洲AV无码久久精品蜜桃| 狠狠色婷婷久久一区二区| 久久精品国产亚洲αv忘忧草| 亚洲午夜无码AV毛片久久| 久久久国产视频| 无码超乳爆乳中文字幕久久| 一本色道久久99一综合| 日韩精品久久无码人妻中文字幕| 久久一日本道色综合久久| 潮喷大喷水系列无码久久精品| 久久精品天天中文字幕人妻| 久久精品国产秦先生| 亚洲午夜久久影院| 人妻中文久久久久| 国产成人无码精品久久久性色 | 人人妻久久人人澡人人爽人人精品| 色综合合久久天天给综看| 中文字幕无码久久久| 一本色道久久88精品综合| 国产一区二区精品久久| 久久婷婷色综合一区二区| 久久狠狠爱亚洲综合影院| 精品久久久久久成人AV| 精品综合久久久久久88小说| 亚洲AV日韩精品久久久久久| 91精品国产91热久久久久福利| 久久久久99精品成人片牛牛影视| 久久亚洲精品成人无码网站| 97久久超碰国产精品2021| 久久久久综合中文字幕| 久久狠狠高潮亚洲精品| 久久婷婷午色综合夜啪| 亚洲国产精品久久久久| 日本欧美久久久久免费播放网|