-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock-based_SPSC_array2.cpp
More file actions
44 lines (39 loc) · 1.09 KB
/
lock-based_SPSC_array2.cpp
File metadata and controls
44 lines (39 loc) · 1.09 KB
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
#include <iostream>
#include <thread>
// Unlike lock-based_SPSC_array this does not use count and use two mutex
// In this case maximum capacity is BUFFER_SIZE-1
template <typename T, size_t BUFFER_SIZE>
class SPSC{
private:
T buffer[BUFFER_SIZE];
std::condition_variable c_cv;
std::condition_variable p_cv;
std::mutex c_mtx;
std::mutex p_mtx;
int p_idx = 0; // index to produce next item
int c_idx = 0; // last index whose item is consumed
public:
void produce(T& value){
{
std::unique_lock<std::mutex> mymtx(p_mtx);
p_cv.wait(p_mtx, [this] {return !((p_idx+1)%BUFFER_SIZE == c_idx);})
buffer[p_idx] = value;
p_idx = (p_idx+1)%BUFFER_SIZE;
}
c_cv.notify_one();
}
T consume(){
T val;
{
std::unique_lock<std::mutex> mymtx(c_mtx);
c_cv.wait(c_mtx, [this] {return !(p_idx==c_idx);})
val = buffer[c_idx+1];
c_idx = (c_idx+1)%BUFFER_SIZE;
}
p_cv.notify_one();
return val;
}
};
int main(){
return 0;
}