什么是 asio::io_service?

What is asio::io_service?

我正在研究异步I/O。我知道 select()、poll() 和 epoll()。 我很好奇的是使用 boost::asio 时的异步 I/O。作为调查的结果,使用了io_service。 select()、poll() 和 epoll 中的哪一个?

asio::io_service 已过时。

在较新的 boost 版本中,它被替换为 asio::io_context。

io_context(形式为io_service)主要目的是对 asio 库中的对象执行异步操作,例如。套接字、接受器等,根据 documentation:

...Synchronous operations on I/O objects implicitly run the io_context object for an individual operation. The io_context functions run(), run_one(), run_for(), run_until(), poll() or poll_one() must be called for the io_context to perform asynchronous operations on behalf of a C++ program. Notification that an asynchronous operation has completed is delivered by invocation of the associated handler. Handlers are invoked only by a thread that is currently calling any overload of run(), run_one(), run_for(), run_until(), poll() or poll_one() for the io_context....

基本上,您将要执行的任务发送给 io_context(async_... 函数),io_context 负责执行这些任务(可能在线程或线程池上)并且调用提供的回调 (=handler)。

它通常在内部使用某些版本的 task-stealing 以在线程池中的线程上高效分配任务。