]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // timer.cpp | |
3 | // ~~~~~~~~~ | |
4 | // | |
b32b8144 | 5 | // Copyright (c) 2003-2017 Christopher M. Kohlhoff (chris at kohlhoff dot com) |
7c673cae FG |
6 | // |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #include <iostream> | |
12 | #include <boost/asio.hpp> | |
13 | #include <boost/bind.hpp> | |
14 | #include <boost/date_time/posix_time/posix_time.hpp> | |
15 | ||
16 | class printer | |
17 | { | |
18 | public: | |
b32b8144 | 19 | printer(boost::asio::io_context& io) |
7c673cae FG |
20 | : timer_(io, boost::posix_time::seconds(1)), |
21 | count_(0) | |
22 | { | |
23 | timer_.async_wait(boost::bind(&printer::print, this)); | |
24 | } | |
25 | ||
26 | ~printer() | |
27 | { | |
28 | std::cout << "Final count is " << count_ << std::endl; | |
29 | } | |
30 | ||
31 | void print() | |
32 | { | |
33 | if (count_ < 5) | |
34 | { | |
35 | std::cout << count_ << std::endl; | |
36 | ++count_; | |
37 | ||
38 | timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1)); | |
39 | timer_.async_wait(boost::bind(&printer::print, this)); | |
40 | } | |
41 | } | |
42 | ||
43 | private: | |
44 | boost::asio::deadline_timer timer_; | |
45 | int count_; | |
46 | }; | |
47 | ||
48 | int main() | |
49 | { | |
b32b8144 | 50 | boost::asio::io_context io; |
7c673cae FG |
51 | printer p(io); |
52 | io.run(); | |
53 | ||
54 | return 0; | |
55 | } |