-
Notifications
You must be signed in to change notification settings - Fork 0
/
produceBall.cpp
61 lines (44 loc) · 1.21 KB
/
produceBall.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
#include <iostream>
#include <mutex>
#include <thread>
#include <condition_variable>
std::condition_variable con;
std::mutex mutex;
int red_ball_count = 0; //已经生产的红球
int blue_ball_count = 0; //已经生产的蓝球
const int red_ball_limit = 2; //每组红球最大数量
const int blue_ball_limit = 1; //每组蓝球最大数量
void resetCount(){
if(red_ball_count == red_ball_limit && blue_ball_count == blue_ball_limit){
red_ball_count = 0;
blue_ball_count = 0;
}
return;
}
void produceRed(){
while(1){
std::unique_lock<std::mutex> lock(mutex);
con.wait(lock, []{ return red_ball_count < red_ball_limit;});
std::cout << "红球" << std::flush;
red_ball_count++;
resetCount();
con.notify_all();
}
}
void produceBlue(){
while(1){
std::unique_lock<std::mutex> lock(mutex);
con.wait(lock, []{ return blue_ball_count < blue_ball_limit;});
std::cout << "蓝球" << std::flush;
blue_ball_count++;
resetCount();
con.notify_all();
}
}
int main(){
std::thread thr1(produceRed);
std::thread thr2(produceBlue);
thr1.join();
thr2.join();
return 0;
}