As you can see here in this example UDP server, the run method will keep the application running forever. (tested)
So there is no need to use the work class as mentioned in the documentation
From boost documentation:
The work class is used to inform the io_service
when work starts and finishes. This ensures that the io_service
object's run()
function will not exit while work is underway, and that it does exit when there is no unfinished work remaining.
At its core, Boost Asio provides a task execution framework that you can use to perform operations of any kind. You create your tasks as function objects and post them to a task queue maintained by Boost Asio. You enlist one or more threads to pick these tasks (function objects) and invoke them.
io_context::run runs until all scheduled tasks are completed. After that io_context::run will return and the caller thread will unblock: boost::asio::io_context io_context; // Schedule some tasks io_context.
The io_service class provides the core I/O functionality for users of the asynchronous I/O objects, including: boost::asio::ip::tcp::socket. boost::asio::ip::tcp::acceptor.
For me, main advantage of Boost. Asio (besides cross-platform work) is, that on each platform, it uses most effective strategy ( epoll on Linux 2.6, kqueue on FreeBSD/MacOSX, Overlapped IO on MS Windows).
The work
class is deprecated and has been replaced by executor_work_guard
. Its purpose has been explained in the documentation:
Some applications may need to prevent an io_context object's
run()
call from returning when there is no more work to do. For example, the io_context may be being run in a background thread that is launched prior to the application's asynchronous operations. Therun()
call may be kept running by creating an object of typeboost::asio::executor_work_guard<io_context::executor_type>
[...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With