boost::asio internal threads

人走茶凉 提交于 2020-01-12 09:16:03

问题


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"

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?


回答1:


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

Explicitly disables Boost.Asio's threading support, independent of whether or not Boost as a whole supports threads.

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.

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.




回答2:


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. Either of those, or a whole bunch of other async event loops provided by various libraries like Qt, could work.



来源:https://stackoverflow.com/questions/14981291/boostasio-internal-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!