3

Is there a particular reason why one should prefer std::async or std::thread over a thread pool?

Basically async is a std::thread with a result and a launch policy, which means the performance of an async will lay behind the pool anyway since thread creation is really expensive.

Now I am curious if there is a reason why I should use async instead of a pool if I am launching tasks very frequently? For long running tasks I definitly see the advantage but for small tasks I can't see any benefit of asyncs at all.

Even for larger tasks I would more likely use a larger thread pool and accept that 1 or 2 threads are busy for a fixed amount of time since I like to have all my threads in one point.

I don't understand in general why there isn't a std::thread_pool implementation out there since I see not much reasons why I should launch threads (with async ot std::thread) during runtime if I can avoid it at all due to the costs of spawning new threads. Can anyone give me a valid reason why I should use asyncs or why there is no thread pool in the standard?

Mango
  • 362
  • 1
  • 14
  • [`std::async`](http://en.cppreference.com/w/cpp/thread/async) *may* create a thread, or it *may* use a thread-pool. Or it may do something different entirely, it's really up to the implementation. – Some programmer dude Jun 20 '17 at 13:03
  • 1
    `std::async`/`std::thread`/threadpool are orthogonal, they can be used to implement one another. the question is flawed – David Haim Jun 20 '17 at 13:03
  • @ Some programmer dude Currently I learned that an async is actually banned from using a thread pool and that MS is violationg this rule. – Mango Jun 20 '17 at 13:06
  • @ David Haim I know that a std::thread is the base for an async, but is there a reason to spawn threads instead of pooling them if you are using them very frequently? Actually pooled threads are not the rule in C++ (at least as far as I know). I never understood why I should manage threads by hand if I could use a pool instead (at least if the threads won't run for a very long period of time) – Mango Jun 20 '17 at 13:09
  • According to [this comment](https://stackoverflow.com/questions/42754244/is-the-visual-c-implementation-of-stdasync-using-a-thread-pool-legal#comment72625988_42754244) on an old question, the default launch policy allows pools. On the same question [another comment](https://stackoverflow.com/questions/42754244/is-the-visual-c-implementation-of-stdasync-using-a-thread-pool-legal#comment72631200_42754244) notices that the term "should" is non-normative and implementation are not required to follow a "should". So while pools are discouraged it's not strictly forbidden. – Some programmer dude Jun 20 '17 at 13:27
  • [Continued] Note that the wording in the standard may have changed since then (the question is from before the C++14 ratification). – Some programmer dude Jun 20 '17 at 13:29

0 Answers0