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.