I wondered why, in this example, io_service in different threads ...

 #include <boost/asio/io_service.hpp> #include <boost/asio/steady_timer.hpp> #include <chrono> #include <thread> #include <iostream> using namespace boost::asio; int main() { io_service ioservice; steady_timer timer1{ioservice, std::chrono::seconds{3}}; timer1.async_wait([](const boost::system::error_code &ec) { std::cout << "3 sec\n"; }); steady_timer timer2{ioservice, std::chrono::seconds{3}}; timer2.async_wait([](const boost::system::error_code &ec) { std::cout << "3 sec\n"; }); std::thread thread1{[&ioservice](){ ioservice.run(); }}; std::thread thread2{[&ioservice](){ ioservice.run(); }}; thread1.join(); thread2.join(); } 

I just tried io_service.run() and once in the main thread - and everything was also asynchronously derived ... what is the profit?

    1 answer 1

    There is no difference, i.e. one of ioservice.run (); You can call in a separate thread, and the second in the main. But then you block the main thread. If both ioservice.run (); you call in separate threads, in the main you can do some work, and then insert to wait for completion.

    • hmm, but is io_service.run () really blocking? - xperious
    • The run () function blocks until it has been finished and there are no more handlers. - Unick
    • understood, thank you ... if so, then yes, you need to call in a separate thread - xperious