[英]Can boost::asio::thread_pool be used instead of combining boost::asio::io_context with a boost::thread::thread_group?
I'm trying to clear up some confusion I have.我正在努力澄清我的一些困惑。 I stumbled over boost::asio::thread_pool and I thought that one could use to somehow automatically combine
boost::asio::io_context
and boost::thread::thread_group
like is often suggested ( here or here ).我偶然发现了boost::asio::thread_pool并且我认为人们可以用某种方式自动组合
boost::asio::io_context
和boost::thread::thread_group
就像经常建议的一样( here或here )。 It appears that this asio
-specific pool can be used to post
tasks to but, on the other hand, some networking types like resolver
need to be passed an object io_context
as a constructor parameter which thread_pool
isn't and doesn't derive from.似乎这个特定于
asio
池可用于将任务post
到,但另一方面,某些网络类型(如resolver
需要传递一个对象io_context
作为构造函数参数,而thread_pool
不是也不是从中派生的。
Say you have a single io_context
object, named ioc
.假设您有一个名为
ioc
io_context
对象。
You can create several threads, and call ioc.run()
in each one of them.您可以创建多个线程,并在每个线程中调用
ioc.run()
。 This is a blocking operation that blocks on epoll/select/kqueue.这是一个阻塞操作,阻塞在 epoll/select/kqueue 上。 Note that
ioc
is shareable, and by calling ioc.run()
in several threads, they implicitly belong to a thread pool to be used by ioc
.请注意,
ioc
是可共享的,并且通过在多个线程中调用ioc.run()
,它们隐式属于ioc
使用的线程池。 Let's call this pool io_threadpool
.我们称这个池为
io_threadpool
。
Now create a separate pool of threads called compute
, that do other things.现在创建一个名为
compute
的单独线程池,用于执行其他操作。 Here is what is possible:这是可能的:
You can use ioc
in threads from both pools (except for a few, like restart()
, which require that ioc
is not actively running).您可以在来自两个池的线程中使用
ioc
(除了少数几个,如restart()
,它要求ioc
不主动运行)。
You can make sync I/O calls from any thread.您可以从任何线程进行同步 I/O 调用。
You can invoke an async call like async_read( ..., handler)
from any thread.您可以从任何线程调用
async_read( ..., handler)
等异步调用。 However, the handler is only invoked in one of the io_threadpool
threads.但是,处理程序仅在
io_threadpool
线程之一中调用。
You can dispatch tasks in either thread pool, but if the task is not going to do any I/O, I expect it to be more efficient to dispatch it in the compute pool, because the system doesn't have to wake up the epoll()
/ kqueue()
/ select()
call on which it is blocked.您可以在任一线程池中调度任务,但是如果任务不进行任何 I/O,我希望在计算池中调度它会更有效,因为系统不必唤醒
epoll()
/ kqueue()
/ select()
调用它被阻止。
您应该将io_context.run()
到thread_pool
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.