-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcpp11_23_wait_for.cpp
More file actions
40 lines (34 loc) · 1.04 KB
/
Copy pathcpp11_23_wait_for.cpp
File metadata and controls
40 lines (34 loc) · 1.04 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
/*************************************************************************
> File Name: cpp11_23_wait_for.cpp
> Author: DragonFive
> Mail: [email protected]
> Created Time: 2016年08月02日 星期二 22时03分45秒
************************************************************************/
#include<iostream>
#include<mutex>
#include<thread>
#include<condition_variable>
#include<chrono>
std::mutex mtx;
std::condition_variable cv;
int inputNum;
//一个输入线程接受输入,一个输出点到线程,等待一秒,如果是因为超时被唤醒就输出一个点;
void input()
{
std::cin>>inputNum;
cv.notify_one();
}
int main()
{
std::cout<<"please input a num ,or I will output dots!"<<std::endl;
std::thread inputTh(input);
std::unique_lock<std::mutex> lck(mtx);
while(cv.wait_for(lck,std::chrono::seconds(1))==std::cv_status::timeout)
{
std::cout<<".";
std::cout.flush();//清空输出缓存区;
}
std::cout<<"\nyour entered :"<<inputNum<<std::endl;
inputTh.join();
return 0;
}