summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/asio/src/examples/cpp11/tutorial
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/asio/src/examples/cpp11/tutorial')
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime1/client.cpp57
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime2/server.cpp50
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime3/server.cpp118
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime4/client.cpp52
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime5/server.cpp53
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime6/server.cpp89
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/daytime7/server.cpp159
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/timer1/timer.cpp24
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/timer2/timer.cpp29
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/timer3/timer.cpp43
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/timer4/timer.cpp54
-rw-r--r--3rdparty/asio/src/examples/cpp11/tutorial/timer5/timer.cpp81
12 files changed, 809 insertions, 0 deletions
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime1/client.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime1/client.cpp
new file mode 100644
index 00000000000..9d927759f5c
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime1/client.cpp
@@ -0,0 +1,57 @@
+//
+// client.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <array>
+#include <iostream>
+#include <asio.hpp>
+
+using asio::ip::tcp;
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ if (argc != 2)
+ {
+ std::cerr << "Usage: client <host>" << std::endl;
+ return 1;
+ }
+
+ asio::io_context io_context;
+
+ tcp::resolver resolver(io_context);
+ tcp::resolver::results_type endpoints =
+ resolver.resolve(argv[1], "daytime");
+
+ tcp::socket socket(io_context);
+ asio::connect(socket, endpoints);
+
+ for (;;)
+ {
+ std::array<char, 128> buf;
+ std::error_code error;
+
+ size_t len = socket.read_some(asio::buffer(buf), error);
+
+ if (error == asio::error::eof)
+ break; // Connection closed cleanly by peer.
+ else if (error)
+ throw std::system_error(error); // Some other error.
+
+ std::cout.write(buf.data(), len);
+ }
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime2/server.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime2/server.cpp
new file mode 100644
index 00000000000..971149d6e8d
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime2/server.cpp
@@ -0,0 +1,50 @@
+//
+// server.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <ctime>
+#include <iostream>
+#include <string>
+#include <asio.hpp>
+
+using asio::ip::tcp;
+
+std::string make_daytime_string()
+{
+ using namespace std; // For time_t, time and ctime;
+ time_t now = time(0);
+ return ctime(&now);
+}
+
+int main()
+{
+ try
+ {
+ asio::io_context io_context;
+
+ tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 13));
+
+ for (;;)
+ {
+ tcp::socket socket(io_context);
+ acceptor.accept(socket);
+
+ std::string message = make_daytime_string();
+
+ std::error_code ignored_error;
+ asio::write(socket, asio::buffer(message), ignored_error);
+ }
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime3/server.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime3/server.cpp
new file mode 100644
index 00000000000..339de72d4a7
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime3/server.cpp
@@ -0,0 +1,118 @@
+//
+// server.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <ctime>
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <string>
+#include <asio.hpp>
+
+using asio::ip::tcp;
+
+std::string make_daytime_string()
+{
+ using namespace std; // For time_t, time and ctime;
+ time_t now = time(0);
+ return ctime(&now);
+}
+
+class tcp_connection
+ : public std::enable_shared_from_this<tcp_connection>
+{
+public:
+ typedef std::shared_ptr<tcp_connection> pointer;
+
+ static pointer create(asio::io_context& io_context)
+ {
+ return pointer(new tcp_connection(io_context));
+ }
+
+ tcp::socket& socket()
+ {
+ return socket_;
+ }
+
+ void start()
+ {
+ message_ = make_daytime_string();
+
+ asio::async_write(socket_, asio::buffer(message_),
+ std::bind(&tcp_connection::handle_write, shared_from_this(),
+ asio::placeholders::error,
+ asio::placeholders::bytes_transferred));
+ }
+
+private:
+ tcp_connection(asio::io_context& io_context)
+ : socket_(io_context)
+ {
+ }
+
+ void handle_write(const std::error_code& /*error*/,
+ size_t /*bytes_transferred*/)
+ {
+ }
+
+ tcp::socket socket_;
+ std::string message_;
+};
+
+class tcp_server
+{
+public:
+ tcp_server(asio::io_context& io_context)
+ : io_context_(io_context),
+ acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
+ {
+ start_accept();
+ }
+
+private:
+ void start_accept()
+ {
+ tcp_connection::pointer new_connection =
+ tcp_connection::create(io_context_);
+
+ acceptor_.async_accept(new_connection->socket(),
+ std::bind(&tcp_server::handle_accept, this, new_connection,
+ asio::placeholders::error));
+ }
+
+ void handle_accept(tcp_connection::pointer new_connection,
+ const std::error_code& error)
+ {
+ if (!error)
+ {
+ new_connection->start();
+ }
+
+ start_accept();
+ }
+
+ asio::io_context& io_context_;
+ tcp::acceptor acceptor_;
+};
+
+int main()
+{
+ try
+ {
+ asio::io_context io_context;
+ tcp_server server(io_context);
+ io_context.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime4/client.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime4/client.cpp
new file mode 100644
index 00000000000..15f3d5d4e98
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime4/client.cpp
@@ -0,0 +1,52 @@
+//
+// client.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <array>
+#include <iostream>
+#include <asio.hpp>
+
+using asio::ip::udp;
+
+int main(int argc, char* argv[])
+{
+ try
+ {
+ if (argc != 2)
+ {
+ std::cerr << "Usage: client <host>" << std::endl;
+ return 1;
+ }
+
+ asio::io_context io_context;
+
+ udp::resolver resolver(io_context);
+ udp::endpoint receiver_endpoint =
+ *resolver.resolve(udp::v4(), argv[1], "daytime").begin();
+
+ udp::socket socket(io_context);
+ socket.open(udp::v4());
+
+ std::array<char, 1> send_buf = {{ 0 }};
+ socket.send_to(asio::buffer(send_buf), receiver_endpoint);
+
+ std::array<char, 128> recv_buf;
+ udp::endpoint sender_endpoint;
+ size_t len = socket.receive_from(
+ asio::buffer(recv_buf), sender_endpoint);
+
+ std::cout.write(recv_buf.data(), len);
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime5/server.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime5/server.cpp
new file mode 100644
index 00000000000..f47c9fa713b
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime5/server.cpp
@@ -0,0 +1,53 @@
+//
+// server.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <array>
+#include <ctime>
+#include <iostream>
+#include <string>
+#include <asio.hpp>
+
+using asio::ip::udp;
+
+std::string make_daytime_string()
+{
+ using namespace std; // For time_t, time and ctime;
+ time_t now = time(0);
+ return ctime(&now);
+}
+
+int main()
+{
+ try
+ {
+ asio::io_context io_context;
+
+ udp::socket socket(io_context, udp::endpoint(udp::v4(), 13));
+
+ for (;;)
+ {
+ std::array<char, 1> recv_buf;
+ udp::endpoint remote_endpoint;
+ socket.receive_from(asio::buffer(recv_buf), remote_endpoint);
+
+ std::string message = make_daytime_string();
+
+ std::error_code ignored_error;
+ socket.send_to(asio::buffer(message),
+ remote_endpoint, 0, ignored_error);
+ }
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime6/server.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime6/server.cpp
new file mode 100644
index 00000000000..42c80795a9e
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime6/server.cpp
@@ -0,0 +1,89 @@
+//
+// server.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <array>
+#include <ctime>
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <string>
+#include <asio.hpp>
+
+using asio::ip::udp;
+
+std::string make_daytime_string()
+{
+ using namespace std; // For time_t, time and ctime;
+ time_t now = time(0);
+ return ctime(&now);
+}
+
+class udp_server
+{
+public:
+ udp_server(asio::io_context& io_context)
+ : socket_(io_context, udp::endpoint(udp::v4(), 13))
+ {
+ start_receive();
+ }
+
+private:
+ void start_receive()
+ {
+ socket_.async_receive_from(
+ asio::buffer(recv_buffer_), remote_endpoint_,
+ std::bind(&udp_server::handle_receive, this,
+ asio::placeholders::error,
+ asio::placeholders::bytes_transferred));
+ }
+
+ void handle_receive(const std::error_code& error,
+ std::size_t /*bytes_transferred*/)
+ {
+ if (!error)
+ {
+ std::shared_ptr<std::string> message(
+ new std::string(make_daytime_string()));
+
+ socket_.async_send_to(asio::buffer(*message), remote_endpoint_,
+ std::bind(&udp_server::handle_send, this, message,
+ asio::placeholders::error,
+ asio::placeholders::bytes_transferred));
+
+ start_receive();
+ }
+ }
+
+ void handle_send(std::shared_ptr<std::string> /*message*/,
+ const std::error_code& /*error*/,
+ std::size_t /*bytes_transferred*/)
+ {
+ }
+
+ udp::socket socket_;
+ udp::endpoint remote_endpoint_;
+ std::array<char, 1> recv_buffer_;
+};
+
+int main()
+{
+ try
+ {
+ asio::io_context io_context;
+ udp_server server(io_context);
+ io_context.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/daytime7/server.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/daytime7/server.cpp
new file mode 100644
index 00000000000..9c9c22edeb2
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/daytime7/server.cpp
@@ -0,0 +1,159 @@
+//
+// server.cpp
+// ~~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <array>
+#include <ctime>
+#include <functional>
+#include <iostream>
+#include <memory>
+#include <string>
+#include <asio.hpp>
+
+using asio::ip::tcp;
+using asio::ip::udp;
+
+std::string make_daytime_string()
+{
+ using namespace std; // For time_t, time and ctime;
+ time_t now = time(0);
+ return ctime(&now);
+}
+
+class tcp_connection
+ : public std::enable_shared_from_this<tcp_connection>
+{
+public:
+ typedef std::shared_ptr<tcp_connection> pointer;
+
+ static pointer create(asio::io_context& io_context)
+ {
+ return pointer(new tcp_connection(io_context));
+ }
+
+ tcp::socket& socket()
+ {
+ return socket_;
+ }
+
+ void start()
+ {
+ message_ = make_daytime_string();
+
+ asio::async_write(socket_, asio::buffer(message_),
+ std::bind(&tcp_connection::handle_write, shared_from_this()));
+ }
+
+private:
+ tcp_connection(asio::io_context& io_context)
+ : socket_(io_context)
+ {
+ }
+
+ void handle_write()
+ {
+ }
+
+ tcp::socket socket_;
+ std::string message_;
+};
+
+class tcp_server
+{
+public:
+ tcp_server(asio::io_context& io_context)
+ : io_context_(io_context),
+ acceptor_(io_context, tcp::endpoint(tcp::v4(), 13))
+ {
+ start_accept();
+ }
+
+private:
+ void start_accept()
+ {
+ tcp_connection::pointer new_connection =
+ tcp_connection::create(io_context_);
+
+ acceptor_.async_accept(new_connection->socket(),
+ std::bind(&tcp_server::handle_accept, this, new_connection,
+ asio::placeholders::error));
+ }
+
+ void handle_accept(tcp_connection::pointer new_connection,
+ const std::error_code& error)
+ {
+ if (!error)
+ {
+ new_connection->start();
+ }
+
+ start_accept();
+ }
+
+ asio::io_context& io_context_;
+ tcp::acceptor acceptor_;
+};
+
+class udp_server
+{
+public:
+ udp_server(asio::io_context& io_context)
+ : socket_(io_context, udp::endpoint(udp::v4(), 13))
+ {
+ start_receive();
+ }
+
+private:
+ void start_receive()
+ {
+ socket_.async_receive_from(
+ asio::buffer(recv_buffer_), remote_endpoint_,
+ std::bind(&udp_server::handle_receive, this,
+ asio::placeholders::error));
+ }
+
+ void handle_receive(const std::error_code& error)
+ {
+ if (!error)
+ {
+ std::shared_ptr<std::string> message(
+ new std::string(make_daytime_string()));
+
+ socket_.async_send_to(asio::buffer(*message), remote_endpoint_,
+ std::bind(&udp_server::handle_send, this, message));
+
+ start_receive();
+ }
+ }
+
+ void handle_send(std::shared_ptr<std::string> /*message*/)
+ {
+ }
+
+ udp::socket socket_;
+ udp::endpoint remote_endpoint_;
+ std::array<char, 1> recv_buffer_;
+};
+
+int main()
+{
+ try
+ {
+ asio::io_context io_context;
+ tcp_server server1(io_context);
+ udp_server server2(io_context);
+ io_context.run();
+ }
+ catch (std::exception& e)
+ {
+ std::cerr << e.what() << std::endl;
+ }
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/timer1/timer.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/timer1/timer.cpp
new file mode 100644
index 00000000000..37577dcb65f
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/timer1/timer.cpp
@@ -0,0 +1,24 @@
+//
+// timer.cpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <iostream>
+#include <asio.hpp>
+
+int main()
+{
+ asio::io_context io;
+
+ asio::steady_timer t(io, asio::chrono::seconds(5));
+ t.wait();
+
+ std::cout << "Hello, world!" << std::endl;
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/timer2/timer.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/timer2/timer.cpp
new file mode 100644
index 00000000000..c1620d78fa5
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/timer2/timer.cpp
@@ -0,0 +1,29 @@
+//
+// timer.cpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <iostream>
+#include <asio.hpp>
+
+void print(const std::error_code& /*e*/)
+{
+ std::cout << "Hello, world!" << std::endl;
+}
+
+int main()
+{
+ asio::io_context io;
+
+ asio::steady_timer t(io, asio::chrono::seconds(5));
+ t.async_wait(&print);
+
+ io.run();
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/timer3/timer.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/timer3/timer.cpp
new file mode 100644
index 00000000000..990ea069739
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/timer3/timer.cpp
@@ -0,0 +1,43 @@
+//
+// timer.cpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <functional>
+#include <iostream>
+#include <asio.hpp>
+
+void print(const std::error_code& /*e*/,
+ asio::steady_timer* t, int* count)
+{
+ if (*count < 5)
+ {
+ std::cout << *count << std::endl;
+ ++(*count);
+
+ t->expires_at(t->expiry() + asio::chrono::seconds(1));
+ t->async_wait(std::bind(print,
+ asio::placeholders::error, t, count));
+ }
+}
+
+int main()
+{
+ asio::io_context io;
+
+ int count = 0;
+ asio::steady_timer t(io, asio::chrono::seconds(1));
+ t.async_wait(std::bind(print,
+ asio::placeholders::error, &t, &count));
+
+ io.run();
+
+ std::cout << "Final count is " << count << std::endl;
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/timer4/timer.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/timer4/timer.cpp
new file mode 100644
index 00000000000..7bfdd3e43a8
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/timer4/timer.cpp
@@ -0,0 +1,54 @@
+//
+// timer.cpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <functional>
+#include <iostream>
+#include <asio.hpp>
+
+class printer
+{
+public:
+ printer(asio::io_context& io)
+ : timer_(io, asio::chrono::seconds(1)),
+ count_(0)
+ {
+ timer_.async_wait(std::bind(&printer::print, this));
+ }
+
+ ~printer()
+ {
+ std::cout << "Final count is " << count_ << std::endl;
+ }
+
+ void print()
+ {
+ if (count_ < 5)
+ {
+ std::cout << count_ << std::endl;
+ ++count_;
+
+ timer_.expires_at(timer_.expiry() + asio::chrono::seconds(1));
+ timer_.async_wait(std::bind(&printer::print, this));
+ }
+ }
+
+private:
+ asio::steady_timer timer_;
+ int count_;
+};
+
+int main()
+{
+ asio::io_context io;
+ printer p(io);
+ io.run();
+
+ return 0;
+}
diff --git a/3rdparty/asio/src/examples/cpp11/tutorial/timer5/timer.cpp b/3rdparty/asio/src/examples/cpp11/tutorial/timer5/timer.cpp
new file mode 100644
index 00000000000..61830691e7a
--- /dev/null
+++ b/3rdparty/asio/src/examples/cpp11/tutorial/timer5/timer.cpp
@@ -0,0 +1,81 @@
+//
+// timer.cpp
+// ~~~~~~~~~
+//
+// Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#include <functional>
+#include <iostream>
+#include <thread>
+#include <asio.hpp>
+
+class printer
+{
+public:
+ printer(asio::io_context& io)
+ : strand_(asio::make_strand(io)),
+ timer1_(io, asio::chrono::seconds(1)),
+ timer2_(io, asio::chrono::seconds(1)),
+ count_(0)
+ {
+ timer1_.async_wait(asio::bind_executor(strand_,
+ std::bind(&printer::print1, this)));
+
+ timer2_.async_wait(asio::bind_executor(strand_,
+ std::bind(&printer::print2, this)));
+ }
+
+ ~printer()
+ {
+ std::cout << "Final count is " << count_ << std::endl;
+ }
+
+ void print1()
+ {
+ if (count_ < 10)
+ {
+ std::cout << "Timer 1: " << count_ << std::endl;
+ ++count_;
+
+ timer1_.expires_at(timer1_.expiry() + asio::chrono::seconds(1));
+
+ timer1_.async_wait(asio::bind_executor(strand_,
+ std::bind(&printer::print1, this)));
+ }
+ }
+
+ void print2()
+ {
+ if (count_ < 10)
+ {
+ std::cout << "Timer 2: " << count_ << std::endl;
+ ++count_;
+
+ timer2_.expires_at(timer2_.expiry() + asio::chrono::seconds(1));
+
+ timer2_.async_wait(asio::bind_executor(strand_,
+ std::bind(&printer::print2, this)));
+ }
+ }
+
+private:
+ asio::strand<asio::io_context::executor_type> strand_;
+ asio::steady_timer timer1_;
+ asio::steady_timer timer2_;
+ int count_;
+};
+
+int main()
+{
+ asio::io_context io;
+ printer p(io);
+ std::thread t([&]{ io.run(); });
+ io.run();
+ t.join();
+
+ return 0;
+}