Commit 37a77a62a010f5f013f24f46f5ffe06f7eee1a9a
1 parent
a9827c4e
Updated second multithreaded version
Showing
1 changed file
with
7 additions
and
1 deletions
14-daytime9.cpp
... | ... | @@ -45,7 +45,7 @@ public: |
45 | 45 | std::cout << "~tcp_connection()" << std::endl; |
46 | 46 | } |
47 | 47 | private: |
48 | - tcp_connection(asio::io_context &io_context) : socket_(io_context) {} | |
48 | + tcp_connection(asio::io_context &io_context) : socket_(io_context), io_context_(io_context) {} | |
49 | 49 | |
50 | 50 | void handle_write(const asio::error_code &error, size_t bytes_transferred) { |
51 | 51 | std::cout << error.category().name() << " : " << error.value() << " : " |
... | ... | @@ -59,6 +59,11 @@ private: |
59 | 59 | void long_operation() |
60 | 60 | { |
61 | 61 | message_ = make_daytime_string(); |
62 | + io_context_.post(std::bind(&tcp_connection::work_done, shared_from_this())); | |
63 | + } | |
64 | + | |
65 | + void work_done() | |
66 | + { | |
62 | 67 | asio::async_write(socket_, asio::buffer(message_), |
63 | 68 | std::bind(&tcp_connection::handle_write, shared_from_this(), |
64 | 69 | std::placeholders::_1, std::placeholders::_2)); |
... | ... | @@ -66,6 +71,7 @@ private: |
66 | 71 | |
67 | 72 | tcp::socket socket_; |
68 | 73 | std::string message_; |
74 | + asio::io_context &io_context_; | |
69 | 75 | }; |
70 | 76 | |
71 | 77 | class tcp_server { | ... | ... |