Search code examples
c++multithreadingasynchronousstdasync

async uses a threadpool?


I am new to C++ and I tried using std::async with launch::async to spawn new threads to handle incoming UDP packets. For every new request on a particular port, async spawns a new thread to handle it.

Under load, I find that the udp packets were reaching me but it takes more than 10 seconds for async to spawn a thread and start processing the information. I was wondering if there is an underlying threadpool and that is the reason, async is blocked and is waiting. If yes, how can I increase the size of this thread pool?


Solution

  • According to the standard std::async cannot use a thread pool because of the requirements on thread local storage. However in practice MSVC does use a thread pool as it's implementation is built on top of PPL and they simply ignore the requirements on thread local storage. Other implementation will launch a new thread for every call to std::async as the language requires.

    As ever Bartosz has an excellent blog post on this subject.