Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 Thread Queue

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.

like image 605
Andrew Spott Avatar asked Mar 01 '26 19:03

Andrew Spott


1 Answers

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

like image 97
Akira Takahashi Avatar answered Mar 04 '26 09:03

Akira Takahashi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!