1 | // Copyright (C) 2001-2003 |
---|---|
2 | // William E. Kempf |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | #define BOOST_THREAD_VERSION 2 |
8 | |
9 | #include <boost/thread/thread_only.hpp> |
10 | #include <boost/thread/xtime.hpp> |
11 | #include <iostream> |
12 | |
13 | struct thread_alarm |
14 | { |
15 | thread_alarm(int secs) : m_secs(secs) { } |
16 | void operator()() |
17 | { |
18 | boost::xtime xt; |
19 | boost::xtime_get(xtp: &xt, clock_type: boost::TIME_UTC_); |
20 | xt.sec += m_secs; |
21 | |
22 | boost::thread::sleep(xt); |
23 | |
24 | std::cout << "alarm sounded..."<< std::endl; |
25 | } |
26 | |
27 | int m_secs; |
28 | }; |
29 | |
30 | int main() |
31 | { |
32 | int secs = 5; |
33 | std::cout << "setting alarm for 5 seconds..."<< std::endl; |
34 | thread_alarm alarm(secs); |
35 | boost::thread thrd(alarm); |
36 | thrd.join(); |
37 | } |
38 |