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