Boost :: thread: How to start all threads, but only up to n works at the same time?

boost::threadIs there any mechanism in the library for controlling how many threads (at best) are running at the same time?

In my case, it would be most convenient to run tags Nat the same time (N can be hundreds or several thousand):

std::vector<boost::thread*> vec;
for (int i = 0; i < N; ++i) {
   vec.push_back(new boost::thread(my_fct));
}
// all are running, now wait for them to finish:
for (int i = 0; i < N; ++i) {
  vec[i]->join();
  delete vec[i];
}

But I want Boost to transparently set a maximum of, say, 4 threads running at a time. (I use an 8-core machine, so I should not run more than 4 at a time.)

Of course, I could take care to start only 4 at a time, but the solution I ask for would be more transparent and convenient.

+3
source share
3

, Boost.Thread , Boost.Threadpool ( ) Boost.Thread, SizePolicy.

, - ( ) threadpool .

+3

, 4 , .

- , , ( , ) , .

, concurrency.

+3

, n . () .

+1

Source: https://habr.com/ru/post/1778513/


All Articles