-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutex_counter.cpp
103 lines (86 loc) · 2.14 KB
/
mutex_counter.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
//How to compile this:
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <chrono>
#include <vector>
class MutexCounter {
public:
MutexCounter() = default;
// Multiple threads/readers can read the counter's value at the same time.
unsigned int read() const {
std::shared_lock<std::shared_mutex> lock(mutex_);
return value_;
}
// Only one thread/writer can increment/write the counter's value.
void increment() {
std::unique_lock<std::shared_mutex> lock(mutex_);
value_++;
}
void decrement() {
std::unique_lock<std::shared_mutex> lock(mutex_);
value_--;
}
private:
mutable std::shared_mutex mutex_;
unsigned int value_ = 0;
};
MutexCounter counter;
MutexCounter counter2;
void thread1Func(void){
int yes = 1;
while(yes){
if(counter2.read()==0){
counter.decrement();
std::cout << "Run this as much as needed" << "\n";
counter.increment();
yes = 0;
}
}
}
void thread2Func(void){
int yes = 1;
while(yes){
if(counter.read()==0){
counter.decrement();
counter2.decrement();
std::cout << "This runs without interruption" << "\n";
counter.increment();
counter2.increment();
yes=0;
}
}
}
int main() {
thread1.push_back(std::thread(thread1Func));
thread1.push_back(std::thread(thread1Func));
thread2.push_back(std::thread(thread2Func));
std::vector<std::thread> thread1vec;
std::vector<std::thread> thread2vec;
int new_image=1;
int new_person=1;
int img_counter=0;
int person_counter=0;
for (int i=0;i<6;i++){
if(new_image){
thread1vec.push_back(std::thread(thread1Func));
thread1vec.at(img_counter).join();
img_counter++;
}
if(new_person){
thread2vec.push_back(std::thread(thread2Func));
thread2vec.at(person_counter).join();
person_counter++;
}
}
// std::thread thread1(thread1Func);
// std::thread thread2(thread2Func);
// std::thread thread3(thread2Func);
// std::thread thread4(thread1Func);
// thread1.join();
// thread2.join();
// thread3.join();
// thread4.join();
std::this_thread::sleep_for (std::chrono::seconds(2));
}