-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path003.multithread_program1.cpp
More file actions
51 lines (47 loc) · 1.22 KB
/
003.multithread_program1.cpp
File metadata and controls
51 lines (47 loc) · 1.22 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
45
46
47
48
49
50
51
/* 参考:https://www.cnblogs.com/ljygoodgoodstudydaydayup/p/5950400.html
* 题目:子线程循环 10 次,接着主线程循环 100 次,
* 接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次。
* ...
* 如此循环50次,试写出代码
* 搞定:学习条件变量condition_variable的使用
*/
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
static std::mutex mtx;
static std::condition_variable cv;
static bool ready = true;
static void func_child(){
int i,j;
for(i=0;i<50;++i){
std::unique_lock<std::mutex> lck(mtx);
while(!ready)cv.wait(lck);
std::cout << "child:";
for(j=0;j<5;++j){
std::cout << j << " ";
}
std::cout << "\n";
ready = false;
cv.notify_one();
}
}
static void func_main(){
int i,j;
for(i=0;i<50;++i){
std::unique_lock<std::mutex> lck(mtx);
while(ready)cv.wait(lck);
std::cout << "main:";
for(j=0;j<37;++j){
std::cout << j << " ";
}
std::cout << "\n";
ready = true;
cv.notify_one();
}
}
void problem1(){
std::thread t1(func_child);
func_main();
t1.join();
}