• <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>
            隨筆-162  評(píng)論-223  文章-30  trackbacks-0
            主類模板
               gcc從4.1.2版本開始提供了__sync_*系列的內(nèi)置API,用于加減和邏輯運(yùn)算,可以對(duì)1,2,4,8字節(jié)長度的數(shù)值或指針類型進(jìn)行原子操作,為方便使用,筆者對(duì)這些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}
            ;
               上面析構(gòu)函數(shù)中,使用了BOOST_STATIC_ASSERT宏來作編譯期約束,確保類型T大小為1,2,4,8字節(jié)長度。由于提供了T到atomic_base的隱式轉(zhuǎn)換和轉(zhuǎn)型操作,因此沒有必要再定義operato +,operator - ,opeator ^,operator |,operator &運(yùn)算符成員或友元重載操作。

            指針類型偏特化
               由于主模板定義了operator+=,operator-=,operator&=,operator|=,operator^=運(yùn)算符重載操作,但當(dāng)T為指針類型時(shí),指針的加減和邏輯運(yùn)算沒有意義,但對(duì)于指針加減一個(gè)數(shù)值是有意義的,因此前兩者操作保留且重定義,但沒有后三者操作,需要特化實(shí)現(xiàn):
             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}
            ;
               同理,上面析構(gòu)函數(shù)中,使用了BOOST_STATIC_ASSERT宏來作編譯期約束,確保類型T的指針大小為1,2,4,8字節(jié)長度。
               
            bool類型完全特化
               由于大小為1字節(jié)的類型包括bool布爾類型,它的取值只有true或false兩種可能,不存在加減運(yùn)算操作,也沒有意義,但邏輯運(yùn)算是有意義的,因此需要特化實(shí)現(xiàn):
             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}
            ;
              
            整數(shù)類型別名   
               針對(duì)常見的基本數(shù)值類型,定義了其類型別名:
             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 春秋十二月 閱讀(4583) 評(píng)論(1)  編輯 收藏 引用 所屬分類: C/C++
            国内精品久久久久影院优| 婷婷久久综合九色综合98| 人妻少妇精品久久| 亚洲欧美精品一区久久中文字幕 | 77777亚洲午夜久久多喷| 人妻无码αv中文字幕久久琪琪布| 久久偷看各类wc女厕嘘嘘| 国产精品无码久久久久久| 亚洲综合久久夜AV | 久久久久久免费一区二区三区| 无码乱码观看精品久久| 国产成人精品久久免费动漫| 久久香综合精品久久伊人| 久久久久久国产精品美女| 久久久久久久人妻无码中文字幕爆| 久久久久97国产精华液好用吗| 99久久99久久精品免费看蜜桃| 久久夜色精品国产噜噜亚洲a| 精品久久一区二区| 亚洲中文字幕无码一久久区| 国产午夜精品久久久久九九| 久久久久久毛片免费播放| 久久久精品人妻一区二区三区蜜桃 | 久久这里只有精品视频99| 四虎国产永久免费久久| 91精品国产9l久久久久| 亚洲国产精品无码久久98| 久久这里的只有是精品23| 久久伊人精品青青草原日本| 精品久久久久久无码中文野结衣 | 久久精品国产99国产精品澳门| 色婷婷综合久久久久中文| 午夜天堂av天堂久久久| 久久久av波多野一区二区| 日产精品99久久久久久| 久久ww精品w免费人成| 久久天天躁狠狠躁夜夜96流白浆| 久久人人爽人人爽人人片AV不| 天堂久久天堂AV色综合| 91久久婷婷国产综合精品青草| 一级做a爰片久久毛片人呢|