forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1116. Print Zero Even Odd.cpp
71 lines (66 loc) · 2.03 KB
/
1116. Print Zero Even Odd.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
//Runtime: 24 ms, faster than 91.61% of C++ online submissions for Print Zero Even Odd.
//Memory Usage: 9.2 MB, less than 100.00% of C++ online submissions for Print Zero Even Odd.
//https://leetcode.com/problems/print-zero-even-odd/discuss/358630/C%2B%2B-Solution
class ZeroEvenOdd {
private:
int n;
mutex mtx;
condition_variable cv;
//used to choose next thread
int next; //-1 for zero, 1 for odd, 0 for even
//the next integer to print
int i;
public:
ZeroEvenOdd(int n) {
this->n = n;
this->next = -1;
this->i = 1;
}
// printNumber(x) outputs "x", where x is an integer.
void zero(function<void(int)> printNumber) {
//need a while loop
while(this->i <= this->n){
unique_lock<mutex> lck(mtx);
cv.wait(lck, [this](){return this->next == -1;});
//need to check whether i is valid before printing
if(this-> i <= this->n){
printNumber(0);
}
//this line should be put outside "if" block!
//, so other threads can exit
this->next = i%2;
//need "unlock" here!
lck.unlock();
cv.notify_all();
}
return;
}
void even(function<void(int)> printNumber) {
while(this->i <= this->n){
unique_lock<mutex> lck(mtx);
cv.wait(lck, [this](){return this->next == 0;});
if(this-> i <= this->n){
printNumber(i);
}
this->i += 1;
this->next = -1;
lck.unlock();
cv.notify_all();
}
return;
}
void odd(function<void(int)> printNumber) {
while(this->i <= this->n){
unique_lock<mutex> lck(mtx);
cv.wait(lck, [this](){return this->next == 1;});
if(this-> i <= this->n){
printNumber(i);
}
this->i += 1;
this->next = -1;
lck.unlock();
cv.notify_all();
}
return;
}
};