-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSRSW.cpp
119 lines (100 loc) · 2.98 KB
/
SRSW.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
//only one thread as the Producer and only one thread as the Consumer
//#pragma once
#include <memory>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <cassert>
using namespace std;
// use default SC memory model
template<typename T, size_t N>
class CircularFifo{
private:
static constexpr uint32_t QSIZE= 2<<N;
static constexpr uint32_t MASK = QSIZE-1;
public:
CircularFifo()
:m_array{new T[QSIZE]},m_tail{0},m_head{0}
{
static_assert(std::is_default_constructible<T>::value,"T does not have a default constructor.");
static_assert(std::is_copy_assignable<T>::value,"T does not support copy assignment.");
static_assert(N!=0,"N is too small.");
cout << "Queue size = " << QSIZE << endl;
}
// the idea is
// if you're the only one to change a variable, you can load with relax, but store it with release to let other know.
// if you're reading some variables potentially changed by others, you need acquire it.
bool push(const T& item) {
const auto head = m_head.load(memory_order_relaxed);
const auto n_head = increment(head);
if (n_head != m_tail.load(memory_order_acquire)) {
m_array[head] = item;
m_head.store(n_head, memory_order_release);
return true;
}
return false;
}
bool pop(T& item) {
const auto tail = m_tail.load(memory_order_relaxed);
if (tail == m_head.load(memory_order_acquire))
return false;
const auto n_tail = increment(tail);
item = m_array[tail];
m_tail.store(n_tail, memory_order_release);
return true;
}
bool isLockFree() const { return true; }
size_t increment(size_t idx) const
{
return (idx + 1) % QSIZE;
}
private:
//size_t increment(size_t idx) const;
std::atomic<size_t> m_tail;
std::unique_ptr<T[]> m_array;
std::atomic<size_t> m_head; // next element to push
};
main() {
CircularFifo<int, 10> dataPipe;
constexpr uint32_t N = 55000;
thread r([&]() {
// random-number generator(1-5) (use i as seed to get different sequences)
std::default_random_engine dre(100);
std::uniform_int_distribution<int> id(1,2);
cout << "starting reader " << endl;
int last = 0;
int current;
for(int i = 0; i < N; ) {
if (dataPipe.pop(current)) {
cout << current << endl;
assert(current == last+1);
++last;
++i;
this_thread::sleep_for(chrono::milliseconds(id(dre)));
}
}
cout << "exiting reader " << endl;
}
);
cout << "starting writer " << endl;
thread w([&]() {
std::default_random_engine dre(200);
std::uniform_int_distribution<int> id(1,5);
for (int i = 1; i <= N; ) {
int loop = id(dre);
for (int j = 0; j < loop && i <= N; )
if(dataPipe.push(i)) {
++i;
++j;
}
this_thread::sleep_for(chrono::milliseconds(3));
}
cout << "exiting writer" << endl;
cout << "Press Enter to exit\n";
cin.get();
});
w.join();
r.join();
cout << "done" << endl;
}