Commit 41ce4ed547230a943433ba51b202c36e04f6f681

Authored by Grzegorz Jabłoński
1 parent fdf44c8d

Updated mutithreaded example

12-threads.cpp
@@ -7,29 +7,29 @@ @@ -7,29 +7,29 @@
7 7
8 int a = 0; 8 int a = 0;
9 9
10 -void WorkerThread(std::shared_ptr<asio::io_context> io_svc, int counter) { 10 +void WorkerThread(std::shared_ptr<asio::io_context> io_context, int counter) {
11 std::cout << counter << ".\n"; 11 std::cout << counter << ".\n";
12 - io_svc->run(); 12 + io_context->run();
13 std::cout << "End.\n"; 13 std::cout << "End.\n";
14 } 14 }
15 15
16 int main(void) { 16 int main(void) {
17 17
18 - auto io_svc = std::make_shared<asio::io_context>(); 18 + auto io_context = std::make_shared<asio::io_context>();
19 19
20 - asio::executor_work_guard work_guard(io_svc->get_executor()); //prevent run() from exiting immediately after all work is done 20 + asio::executor_work_guard work_guard(io_context->get_executor()); //prevent run() from exiting immediately after all work is done
21 21
22 std::cout << "Press ENTER key to exit!" << std::endl; 22 std::cout << "Press ENTER key to exit!" << std::endl;
23 23
24 std::vector<std::thread> threads; 24 std::vector<std::thread> threads;
25 for (int i = 0; i < 5; i++) 25 for (int i = 0; i < 5; i++)
26 - threads.emplace_back(WorkerThread, io_svc, i); 26 + threads.emplace_back(WorkerThread, io_context, i);
27 27
28 - io_svc->post([] { std::cout << "Hello\n"; }); 28 + io_context->post([] { std::cout << "Hello\n"; });
29 29
30 std::cin.get(); 30 std::cin.get();
31 31
32 - io_svc->stop(); 32 + io_context->stop();
33 33
34 for (auto &i : threads) 34 for (auto &i : threads)
35 i.join(); 35 i.join();
13-daytime8.cpp
@@ -95,7 +95,7 @@ int main() { @@ -95,7 +95,7 @@ int main() {
95 try { 95 try {
96 auto io_context = std::make_shared<asio::io_context>(); 96 auto io_context = std::make_shared<asio::io_context>();
97 97
98 - asio::executor_work_guard work_guard(io_svc->get_executor()); //prevent run() from exiting immediately after all work is done 98 + asio::executor_work_guard work_guard(io_context->get_executor()); //prevent run() from exiting immediately after all work is done
99 99
100 std::vector<std::thread> threads; 100 std::vector<std::thread> threads;
101 101