Ticket #8768: wait_until-example.cpp

File wait_until-example.cpp, 1.5 KB (added by Dmitry Lysachenko <xdemonx07@…>, 9 years ago)

repro example

Line 
1#include <boost/chrono.hpp>
2#include <boost/thread.hpp>
3#include <boost/optional.hpp>
4
5template <class Clock>
6class fixed_point_clock
7{
8public:
9 typedef typename Clock::duration duration;
10 typedef typename duration::rep rep;
11 typedef typename duration::period period;
12 typedef boost::chrono::time_point<fixed_point_clock> time_point;
13
14 static time_point now()
15 {
16 if (!fixedPoint.is_initialized())
17 fixedPoint = boost::in_place(Clock::now().time_since_epoch());
18 return *fixedPoint;
19 }
20private:
21 static boost::optional<time_point> fixedPoint;
22};
23template <class Clock>
24boost::optional<typename fixed_point_clock<Clock>::time_point>
25 fixed_point_clock<Clock>::fixedPoint = boost::none;
26
27int main()
28{
29 using namespace std;
30 //any clock can be used, important that we get: t - Clock::t == -1 milliseconds
31 //it easier to achieve with fixed_point_clock
32 //typedef boost::chrono::system_clock clock;
33 typedef fixed_point_clock<boost::chrono::system_clock> clock;
34
35 boost::condition_variable var;
36 boost::mutex mut;
37 boost::mutex::scoped_lock lk(mut);
38
39 auto until = clock::now() - boost::chrono::milliseconds(1);
40
41 //in wait_until: " do_wait(lock, ceil<milliseconds>(t-Clock::now()).count()) "
42 //if ceil<milliseconds>(t-Clock::now()).count() == -1 then boost::detail::timeout::milliseconds == 0xf...f,
43 //witch is a special value - is_sentinel(), it is checked in win32/thread.cpp: this_thread::interruptible_wait
44
45 var.wait_until(lk, until); //infinite wait
46
47 cout << "done" << endl;
48 return EXIT_SUCCESS;
49}