简体   繁体   English

boost :: asio内部线程

[英]boost::asio internal threads

When using boost::asio for some asynchronous TCP communication I noticed it starts a lot of (3-4) internal threads. 当使用boost :: asio进行一些异步TCP通信时,我注意到它启动了很多(3-4)内部线程。 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"

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() ). 现在我的lib有非常严格的要求,不能启动任何额外的线程(除了客户端提供的线程,现在启动io_service::run() )。 Is there any way to stop boost::asio from creating these extra threads? 有没有办法阻止boost :: asio创建这些额外的线程?

Alternatively, is there any other async library out there that can run in only one thread? 或者,是否有任何其他异步库可以只在一个线程中运行?

You can disable the emulated asynchronous operation support by defining BOOST_ASIO_DISABLE_THREADS in the appropriate translation units. 您可以通过在适当的转换单元中定义BOOST_ASIO_DISABLE_THREADS来禁用模拟的异步操作支持。 The documentation has this to say about the definition 文档中有关于定义的说法

Explicitly disables Boost.Asio's threading support, independent of whether or not Boost as a whole supports threads. 显式禁用Boost.Asio的线程支持,不管Boost作为一个整体是否支持线程。

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() . 例如,我知道几乎每个平台都以这种方式模拟async_resolve() ,该线程是在第一次调用async_resolve() Some (all?) Windows platforms emulate several other operations such as deadline_timer operations. 一些(所有?)Windows平台模拟其他几个操作,例如deadline_timer操作。

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_DISABLE_THREADS ,所以我不确定它是否有其他副作用。

Have you actually tested it to see if threads are spawned on your particular platform? 您是否实际对其进行了测试,以确定是否在您的特定平台上生成了线程? If none are, problem solved! 如果没有,问题就解决了! If some are, you might consider a different library like libevent or libev. 如果有的话,你可以考虑使用像libevent或libev这样的不同库。 Either of those, or a whole bunch of other async event loops provided by various libraries like Qt, could work. 这些中的任何一个,或者像Qt这样的各种库提供的一大堆其他异步事件循环都可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM