码迷,mamicode.com
首页 > 编程语言 > 详细

仿Java的AtomicMarkableReference的AtomicMarkablePointer(C++)

时间:2014-05-12 01:24:20      阅读:504      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

bubuko.com,布布扣
 1 //@author: Zou Xiaohang
 2 //@describe: this class is like AtomicMarkableReference which is in java concurrent package.
 3 //             remember the pointer is 32bits address
 4 template<class Pointer>
 5 class AtomicMarkablePointer{
 6 private:
 7     std::atomic<Pointer> ptr;
 8     const int32_t Marked = 0x00000001;
 9     const int32_t UnMraked = 0x00000000;
10 public:
11     explicit AtomicMarkablePointer(Pointer p, bool mark){
12         ptr.store((Pointer)((int32_t)p | (int32_t)mark));
13     }
14     AtomicMarkablePointer(const AtomicMarkablePointer &a){
15         ptr.store(a.ptr.load());
16     }
17     AtomicMarkablePointer operator = (const AtomicMarkablePointer &a){
18         if (*this != a){
19             ptr.store(a.ptr.load());
20         }
21         return *this;
22     }
23     Pointer getReference() const {
24         Pointer p = ptr.load();
25         return (bool)((int32_t)p & Marked) ? (Pointer)((int32_t)(p)& ~Marked) : p;
26     }
27     bool isMarked() const {
28         Pointer p = ptr.load();
29         return (bool)((int32_t)p & Marked);
30     }
31     Pointer get(bool &b) const {
32         Pointer p = ptr.load();
33         b = (bool)((int32_t)p & Marked);
34         return b ? (Pointer)((int32_t)(p)& ~Marked) : p;
35     }
36     bool compareAndSet(Pointer expectedPointer, Pointer newPointer,
37         bool expectedMark, bool newMark){
38         Pointer p = ptr.load();
39         bool b = (bool)((int32_t)p & Marked);
40         if (b == expectedMark){
41             expectedPointer = (Pointer)((int32_t)expectedPointer | (int32_t)b);
42             return ptr.compare_exchange_strong(expectedPointer, (Pointer)((int32_t)newPointer | (int32_t)newMark));
43         }
44         return false;
45     }
46     void set(Pointer newPointer, bool newMark){
47         newPointer = (Pointer)((int32_t)newPointer | (int32_t)newMark);
48         ptr.exchange(newPointer);
49     }
50     bool attemptMark(Pointer expectedPointer, bool newMark){
51         Pointer newPointer = (Pointer)((int32_t)expectedPointer | (int32_t)newMark);
52         expectedPointer = isMarked() ? (Pointer)((int32_t)expectedPointer | Marked) : expectedPointer;
53         return ptr.compare_exchange_strong(expectedPointer, newPointer);
54     }
55     /*void print()const{
56         std::cout << getReference() << " ";
57         std::cout << (isMarked() ? "Marked" : "UnMarked");
58     }*/
59 };
bubuko.com,布布扣

 

仿Java的AtomicMarkableReference的AtomicMarkablePointer(C++),布布扣,bubuko.com

仿Java的AtomicMarkableReference的AtomicMarkablePointer(C++)

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/zxh1210603696/p/3721460.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!