wangzhengquan
2020-05-25 ff31a5b78ebe4b4348ed7fd572941b23a87414c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "common.h"
#include "squeue.h"
using namespace std;
 
class Item {
public:
 
    long pic;
    long info;
    Item(int _pic = 0, int _info = 0) noexcept : pic(_pic), info(_info)  {}
};
 
 
 
int main () {
    void * shmp;
    int first = mm_init(sizeof(SQueue<struct Item>), &shmp);
    int b = 5;
    std::atomic<int> a(b);
 
    if (a.compare_exchange_weak(
            b, 
            b + 1, 
            std::memory_order_release, 
            std::memory_order_relaxed) ) {
 
        cout << "true:" << a.load() << endl;
    } else {
        cout << "false: " << b << endl;
    }
 
    
     
    Item item(1,1);
    //item.pic = 0;
    //item.info = 0;
    std::atomic<Item > aitem;
    aitem.store(item, std::memory_order_relaxed);
     
    Item item1(1, 1);
 
    Item item2(2, 2);
    // item2.pic = 0;
    // item2.info = 0;
    
 
    if (aitem.compare_exchange_strong(item1, 
                    Item(2, 2), 
                    std::memory_order_release, 
                    std::memory_order_relaxed) ) {
        cout << "true:" << aitem.load( std::memory_order_relaxed).pic << endl;
 
    } else {
        cout << "false: " << aitem.load( std::memory_order_relaxed).pic << endl;
    }
 
    std::atomic<Item > aitem2;
    cout << "false: " << aitem2.load( std::memory_order_relaxed).pic << endl;
 
    
    std::atomic<Pointer<Item> > Head;       // pointer to front of Queue
    std::atomic<Pointer<Item> > Tail;        // pointer to rear of Queue
    Node<Item> *node = new Node<Item>;
    Pointer<Item> ptr(node, 0);
    
    Head.store(ptr, std::memory_order_relaxed);
    Tail.store(ptr, std::memory_order_relaxed);
 
 
     Pointer<Item> tail = Tail.load(std::memory_order_relaxed);
     cout << "node addr " << node << " next addr " << &(tail.ptr->next) << endl;
    Pointer<Item> next = (tail.ptr->next).load(std::memory_order_relaxed);
    // if ((tail.ptr->next).compare_exchange_weak(next, 
 //                    Pointer<T>(node, next.count+1), 
 //                    std::memory_order_release, 
 //                    std::memory_order_relaxed) ) {
 
    // }
 
    return 0;
}