-
Notifications
You must be signed in to change notification settings - Fork 0
/
aba_fixed.cpp
158 lines (137 loc) · 3.37 KB
/
aba_fixed.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <atomic>
#include <exception>
#include <iostream>
#include <memory>
#include <random>
#include <shared_mutex>
#include <stdexcept>
#include <thread>
template <class T>
class atomic_shared_ptr
{
private:
std::shared_ptr<T> p;
public:
atomic_shared_ptr(T* ptr) : p(ptr) {}
atomic_shared_ptr(atomic_shared_ptr<T> const& oth) = delete;
atomic_shared_ptr(std::shared_ptr<T> const& ptr) : p(ptr) {}
atomic_shared_ptr(std::shared_ptr<T>&& ptr) : p(std::move(ptr)) {}
atomic_shared_ptr& operator=(atomic_shared_ptr<T> const& oth) = delete;
atomic_shared_ptr<T>& operator=(std::shared_ptr<T> const& ptr)
{
store(ptr);
return *this;
}
operator std::shared_ptr<T>() const
{
return load();
}
void store(std::shared_ptr<T> const& ptr,
std::memory_order ord = std::memory_order_seq_cst)
{
std::atomic_store_explicit(&p, ptr, ord);
}
std::shared_ptr<T> load(std::memory_order ord = std::memory_order_seq_cst) const
{
return std::atomic_load_explicit(&p, ord);
}
bool compare_exchange_weak(std::shared_ptr<T>& expected,
std::shared_ptr<T> const& desired)
{
return atomic_compare_exchange_weak(&p, &expected, desired);
}
};
template <typename T>
struct node
{
T data;
std::shared_ptr<node<T>> next;
node(const T& data) : data(data), next(nullptr) {}
};
template <typename T>
class stack
{
atomic_shared_ptr<node<T>> head{nullptr};
public:
~stack()
{
while (head.load(std::memory_order_relaxed))
{
pop();
}
}
void push(const T& data)
{
std::shared_ptr<node<T>> new_node = std::make_shared<node<T>>(data);
// std::cout << "add node(" << data << ") at: " << new_node << "\n";
new_node->next = head.load(std::memory_order_relaxed);
while (!head.compare_exchange_weak(new_node->next, new_node))
; // the body of the loop is empty
}
T pop()
{
std::shared_ptr<node<T>> old_head = head.load();
while (old_head && !head.compare_exchange_weak(old_head, old_head->next))
;
if (old_head)
{
T tmp = std::move(old_head->data);
// std::cout << "pop: " << tmp << " (" << tr << " try) at: " << old_head << "\n";
return tmp;
}
throw std::out_of_range("stack is empty");
}
template <class T2>
friend std::ostream& operator<<(std::ostream& out, stack<T2> const& s);
};
int main(int argc, char* argv[])
{
size_t nb = 1;
if (argc >= 2)
{
// std::cout << "usage: " << argv[0] << " nb [defer=false]\n";
nb = std::stoul(argv[1]);
}
std::shared_mutex mut;
for (size_t i = 0; i < nb; i++)
{
// std::cout << "============================\n";
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
mut.lock();
std::thread t1([&] {
std::shared_lock lck(mut);
s.pop();
});
std::thread t2([&] {
std::shared_lock lck(mut);
s.pop();
s.pop();
s.push(5);
});
mut.unlock();
t1.join();
t2.join();
std::cout << s << "\n";
// std::cout << "-----------------------------\n";
}
}
template <class T>
std::ostream& operator<<(std::ostream& out, stack<T> const& s)
{
std::shared_ptr<node<T>> cur = s.head.load(std::memory_order_relaxed);
while (cur)
{
out << cur->data;
if (cur->next)
{
out << ", ";
}
cur = cur->next;
}
return out;
}