boost :: asio内部线程 [英] boost::asio internal threads
问题描述
当使用boost :: asio进行一些异步TCP通信时,我注意到它启动了很多(3-4)内部线程.阅读文档,它说
When using boost::asio for some asynchronous TCP communication I noticed it starts a lot of (3-4) internal threads. Reading in the documentation, it says
"The implementation of this library for a particular platform may
make use of one or more internal threads to emulate asynchronicity"
现在,我的库对不启动任何额外的线程有非常严格的要求(客户端提供的线程除外,该线程现在启动io_service::run()
).有什么方法可以阻止boost :: asio创建这些额外的线程?
Now my lib has very strict requirements to not start any extra threads (except one that is supplied by the client and which now starts io_service::run()
). Is there any way to stop boost::asio from creating these extra threads?
或者,还有其他异步库只能在一个线程中运行吗?
Alternatively, is there any other async library out there that can run in only one thread?
推荐答案
您可以通过在相应的转换单元中定义BOOST_ASIO_DISABLE_THREADS
来禁用仿真的异步操作支持. 文档的内容是这样的:
You can disable the emulated asynchronous operation support by defining BOOST_ASIO_DISABLE_THREADS
in the appropriate translation units. The documentation has this to say about the definition
明确禁用Boost.Asio的线程 支持,与Boost整体是否支持无关 线程.
Explicitly disables Boost.Asio's threading support, independent of whether or not Boost as a whole supports threads.
如果找不到平台具体的实施说明,其中明确说明了哪些操作使用了此仿真.例如,我知道几乎每个平台上的async_resolve()
都以这种方式进行仿真,该线程是在第一次调用async_resolve()
时创建的.某些(全部?)Windows平台模拟了其他一些操作,例如deadline_timer
操作.
If you didn't find the platform specific implementation notes, it clearly states which operations use this emulation. For example I know on nearly every platform async_resolve()
is emulated in this fashion, the thread is created upon the first invocation of async_resolve()
. Some (all?) Windows platforms emulate several other operations such as deadline_timer
operations.
禁用线程支持的一种替代方法可能是避免这些模拟操作.我个人没有在项目中使用BOOST_ASIO_DISABLE_THREADS
,所以不确定它是否还有其他副作用.
One alternative to disabling threading support might be to avoid these emulated operations. I personally have not used BOOST_ASIO_DISABLE_THREADS
in a project so I'm unsure if it has other side effects.
这篇关于boost :: asio内部线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!