-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_wait_for.cpp
More file actions
58 lines (47 loc) · 1.22 KB
/
test_wait_for.cpp
File metadata and controls
58 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
52
53
54
55
56
57
58
#if __cplusplus < 201103L
// see https://svn.boost.org/trac10/ticket/13599
# define BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC
# define BOOST_THREAD_USES_CHRONO
# include <boost/thread.hpp>
using namespace boost;
#else
// see https://en.cppreference.com/w/cpp/thread/condition_variable/wait_for
# include "simple_stopwatch.hpp"
# include <chrono>
# include <condition_variable>
using namespace std;
#endif
#include <csignal>
#include <iostream>
#include <unistd.h>
void handler(int signum)
{
switch (signum) {
case SIGALRM:
signal(signum, SIG_DFL);
break;
default: // ignored
break;
}
}
volatile bool flag = { false };
bool predicate() { return flag; }
int main(int /*argc*/, char* /*argv*/[])
{
signal(SIGALRM, &handler);
::alarm(1); // s
condition_variable cv;
mutex m;
unique_lock<mutex> lock(m);
{
#if __cplusplus >= 201103L
StopwatchReporter sw;
#endif
if (cv.wait_for(lock, chrono::seconds(2), &predicate)) {
std::cerr << "ERROR: timeout expected after 2s!" << std::endl;
return EXIT_FAILURE;
}
}
std::cout << "wait_for has returned with timout" << std::endl;
return EXIT_SUCCESS;
}