3

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?

sethu
  • 1,691
  • 4
  • 22
  • 34
  • 1
    10 seconds is a ridiculous amount of time to spawn a thread, you are probably hitting some kind of limit on OS resources. As an aside I would recommend looking at [boost.ASIO](http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio.html) for an excellent library to use for task based parallelism and networking and one which has been proposed for inclusion in the standard. – sjdowling Apr 14 '15 at 08:25
  • I added more instrumentation to the code and identified that the issue was not with async. After the poll, the packet was picked for processing only after 10 seconds. I still dont understand why there was a huge delay for the poll in the first place. – sethu Apr 19 '15 at 04:27

1 Answers1

4

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.

sjdowling
  • 2,994
  • 2
  • 21
  • 31