3

I would like to be able to launch a whole bunch of threads:

futures_que< std::future< ret_value > > fq;

for ( auto a: some_very_large_container )
    fq.push_back( std::async( std::launch::async, some_computationally_expensive_function, a));

std::vector< ret_value > values;
for ( auto f: fq ) {
    f.wait();
    values.push_back( f.get() );
}

However, if I do this naively (say with futures_que being a std::vector), they all run at once, and things aren't very efficient. How do I do something similar: launch all the threads, but only run a few (say the number of cores on my computer), and when one thread dies, start another.

Jorge Israel Peña
  • 36,800
  • 16
  • 93
  • 123
Andrew Spott
  • 3,457
  • 8
  • 33
  • 59
  • 3
    it is called thread pool – Bryan Chen Feb 27 '14 at 05:27
  • @BryanChen Not really. A classic thread pool has a fixed number of threads running, and they use message passing to ask for and be assigned new tasks. I'm ok with the thread itself being destroyed/created. I just want a fixed number of threads. That is a subtle difference (I don't have to worry about creating a message passing framework with my way). – Andrew Spott Feb 27 '14 at 05:34
  • 1
    Creating/destroying threads and running permanent threads are just two different implementations of thread pools. Thread creation is very expensive especially if your tasks are small and thread creation happens often. What you need is definitely a thread pool. Message passing framework??? There are many kind of "message passing frameworks" for thread pools but one very simple implementation is called "blocking queue". It is very easy to implement. – pasztorpisti Feb 27 '14 at 05:54
  • Are you asking how to write a for loop that uses a counter variable, rather than the relatively new container syntax? :) – Kenny Ostrom Apr 11 '17 at 13:01
  • C# will solve this for you ;) In C++, I think you cannot fire-and-forget. You will have to implement a controller managing the threads. You can probably use the `when_any` to know when to kick-off a new thread – JHBonarius Apr 11 '17 at 13:18

2 Answers2

1

Standard C++ will add when_any() function as future's queue. Please see follow paper.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3784.pdf

Akira Takahashi
  • 2,912
  • 22
  • 107
0

passing std::launch::async | std::launch::deferred to std::async like

std::async(std::launch::async | std::launch::deferred, some_computationally_expensive_function)

did the trick in Visual Studio 2013, but doesn't do since Visual Studio 2015.