-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathexerex-countdown-timer-b.cpp
More file actions
61 lines (37 loc) · 1023 Bytes
/
Copy pathexerex-countdown-timer-b.cpp
File metadata and controls
61 lines (37 loc) · 1023 Bytes
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
/*
COUNTDOWN TIMER
*/
#include <iostream>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
using namespace std;
char* buffer = nullptr;
void* userInputFunc(void*) {
cin.getline(buffer, 1024);
pthread_exit(nullptr);
return nullptr;
}
int main() {
constexpr int SECONDS = 5;
char buff[1024] = { 0 };
buffer = buff;
pthread_t tid;
timespec ts;
int ret = 0;
cout << "You have " << SECONDS << " seconds to write anything you like in one line." << endl;
cout << "Press enter to start." << endl;
cin.getline(buffer, 1024);
cout << "START!!!" << endl << endl;
ret = pthread_create(&tid, nullptr, &userInputFunc, nullptr);
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += SECONDS;
if (ret = pthread_timedjoin_np(tid, nullptr, &ts)) {
ret = pthread_cancel(tid); // Kill thread
cout << "\n\nTIMEOUT!!!" << endl;
}
else {
cout << "\nYou completed before the deadline." << endl;
}
return 0;
}