Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost asio run vs work (ambiguity) - what's the purpose of the work class?

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.

like image 756
Roger World Avatar asked Mar 18 '19 15:03

Roger World


People also ask

How does boost ASIO work?

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.

What does Io_context run do?

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.

What is boost io_service?

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.

Does boost ASIO use Epoll?

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).


1 Answers

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. The run() call may be kept running by creating an object of type boost::asio::executor_work_guard<io_context::executor_type> [...]

like image 117
Jodocus Avatar answered Oct 15 '22 01:10

Jodocus