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). 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.
- 1,367
- 7
- 26
2 Answers
Say you have a single io_context object, named ioc.
You can create several threads, and call ioc.run() in each one of them. This is a blocking operation that blocks on 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. Let's call this pool io_threadpool.
Now create a separate pool of threads called compute, that do other things. Here is what is possible:
You can use
iocin threads from both pools (except for a few, likerestart(), which require thatiocis not actively running).You can make sync I/O calls from any thread.
You can invoke an async call like
async_read( ..., handler)from any thread. However, the handler is only invoked in one of theio_threadpoolthreads.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.
- 2,152
- 2
- 17
- 44
- 1,255
- 17
- 16
You should post your io_context.run() into the thread_pool.
- 2,152
- 2
- 17
- 44
- 460
- 1
- 4
- 11
-
1So... the answer is no? – screwnut Sep 19 '18 at 00:46
-
1Yes, the answer is no. You can learn more about concept-model. – user2709407 Sep 20 '18 at 01:38