Commit a8ad056abc616af5ff55cf6dd5d73942c9c148e2
1 parent
3a0a35ab
Updated mutithreaded example
Showing
1 changed file
with
13 additions
and
6 deletions
13-daytime8.cpp
| ... | ... | @@ -50,9 +50,12 @@ public: |
| 50 | 50 | private: |
| 51 | 51 | tcp_connection(asio::io_context &io_context) : socket_(io_context) {} |
| 52 | 52 | |
| 53 | - void handle_write(const asio::error_code & /*error*/, | |
| 54 | - size_t /*bytes_transferred*/) { | |
| 55 | - std::cout << "Written!" << std::endl; | |
| 53 | + void handle_write(const asio::error_code &error, size_t bytes_transferred) { | |
| 54 | + std::cout << error.category().name() << " : " << error.value() << " : " << error.message() << std::endl; | |
| 55 | + if (error) | |
| 56 | + std::cout << "Error" << std::endl; | |
| 57 | + else | |
| 58 | + std::cout << "Written " << bytes_transferred << " bytes" << std::endl; | |
| 56 | 59 | } |
| 57 | 60 | |
| 58 | 61 | tcp::socket socket_; |
| ... | ... | @@ -90,9 +93,7 @@ private: |
| 90 | 93 | tcp::acceptor acceptor_; |
| 91 | 94 | }; |
| 92 | 95 | |
| 93 | -void WorkerThread(std::shared_ptr<asio::io_context> io_svc) { | |
| 94 | - io_svc->run(); | |
| 95 | -} | |
| 96 | +void WorkerThread(std::shared_ptr<asio::io_context> io_svc) { io_svc->run(); } | |
| 96 | 97 | |
| 97 | 98 | int main() { |
| 98 | 99 | try { |
| ... | ... | @@ -107,6 +108,12 @@ int main() { |
| 107 | 108 | |
| 108 | 109 | tcp_server server(*io_context); |
| 109 | 110 | |
| 111 | + std::cout << "Press ENTER key to exit!" << std::endl; | |
| 112 | + | |
| 113 | + std::cin.get(); | |
| 114 | + | |
| 115 | + io_context->stop(); | |
| 116 | + | |
| 110 | 117 | for (auto &i : threads) |
| 111 | 118 | i.join(); |
| 112 | 119 | ... | ... |