/
timedwait-threads.cc
55 lines (43 loc) · 1.2 KB
/
timedwait-threads.cc
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
#include "timedwait.hh"
#include <thread>
#include <condition_variable>
#include <mutex>
bool quiet = false;
double exit_delay = 0.5;
double timeout = 0.75;
std::mutex m;
std::condition_variable_any alert;
pid_t waitresult = -2;
int waitstatus;
static void child_threadfunc(pid_t p1) {
int status;
pid_t p = waitpid(p1, &status, 0);
assert(p == p1);
std::unique_lock<std::mutex> guard(m);
if (waitresult == -2) {
waitresult = p;
waitstatus = status;
}
alert.notify_all();
}
static void timeout_threadfunc() {
dsleep(timeout);
std::unique_lock<std::mutex> guard(m);
if (waitresult == -2) {
waitresult = 0;
}
alert.notify_all();
}
int main(int argc, char** argv) {
parse_arguments(argc, argv);
double start_time = timestamp();
pid_t p1 = timedwait_make_child(exit_delay);
// We must lock the mutex BEFORE creating threads! (Why?)
std::unique_lock<std::mutex> guard(m);
std::thread child_thread(child_threadfunc, p1);
child_thread.detach();
std::thread delay_thread(timeout_threadfunc);
delay_thread.detach();
alert.wait(guard);
timedwait_print_results(waitresult, waitstatus, timestamp() - start_time);
}