5
votes

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?

2

2 Answers

7
votes

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.

1
votes

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.