]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/example/cpp03/tutorial/timer5/timer.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / asio / example / cpp03 / tutorial / timer5 / timer.cpp
CommitLineData
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/thread/thread.hpp>
14#include <boost/bind.hpp>
15#include <boost/date_time/posix_time/posix_time.hpp>
16
17class printer
18{
19public:
b32b8144 20 printer(boost::asio::io_context& io)
7c673cae
FG
21 : strand_(io),
22 timer1_(io, boost::posix_time::seconds(1)),
23 timer2_(io, boost::posix_time::seconds(1)),
24 count_(0)
25 {
b32b8144
FG
26 timer1_.async_wait(boost::asio::bind_executor(strand_,
27 boost::bind(&printer::print1, this)));
28
29 timer2_.async_wait(boost::asio::bind_executor(strand_,
30 boost::bind(&printer::print2, this)));
7c673cae
FG
31 }
32
33 ~printer()
34 {
35 std::cout << "Final count is " << count_ << std::endl;
36 }
37
38 void print1()
39 {
40 if (count_ < 10)
41 {
42 std::cout << "Timer 1: " << count_ << std::endl;
43 ++count_;
44
45 timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
b32b8144
FG
46
47 timer1_.async_wait(boost::asio::bind_executor(strand_,
48 boost::bind(&printer::print1, this)));
7c673cae
FG
49 }
50 }
51
52 void print2()
53 {
54 if (count_ < 10)
55 {
56 std::cout << "Timer 2: " << count_ << std::endl;
57 ++count_;
58
59 timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
b32b8144
FG
60
61 timer2_.async_wait(boost::asio::bind_executor(strand_,
62 boost::bind(&printer::print2, this)));
7c673cae
FG
63 }
64 }
65
66private:
b32b8144 67 boost::asio::io_context::strand strand_;
7c673cae
FG
68 boost::asio::deadline_timer timer1_;
69 boost::asio::deadline_timer timer2_;
70 int count_;
71};
72
73int main()
74{
b32b8144 75 boost::asio::io_context io;
7c673cae 76 printer p(io);
b32b8144 77 boost::thread t(boost::bind(&boost::asio::io_context::run, &io));
7c673cae
FG
78 io.run();
79 t.join();
80
81 return 0;
82}