Frank
Frank

Reputation: 66174

boost::thread: How to start all threads, but have only up to n running at a time?

In the boost::thread library, is there any mechanism to control how many threads (at most) are running at a time?

In my case, it would be most convenient to start N threads all at the same time (N may be hundreds or a few 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'm sharing an 8-core machine, so I'm not supposed to run more than 4 at a time.)

Of course, I could take care of starting only 4 at a time myself, but the solution I'm asking about would be more transparent and most convenient.

Upvotes: 3

Views: 792

Answers (3)

dan_waterworth
dan_waterworth

Reputation: 6441

You could create a lock that you could only obtain n times. Then each thread should have to obtain the lock (blocking) before processing.

Upvotes: 1

Steve Townsend
Steve Townsend

Reputation: 54148

Don't think Boost.Thread has this built in but you can overlay Boost.Threadpool (not an official library) onto Boost.Thread, and that does allow you to control the thread count via SizePolicy.

The default is a fixed-size pool which is what you want - specify the initial (and ongoing) thread count on the threadpool constructor.

Upvotes: 3

James
James

Reputation: 25523

What you really want, it seems, would be to only ever have 4 threads, each of which would process many jobs.

One way to implement this would be to spawn as many threads as you like, and then for the run-loop of each threads to take tasks (typically function objects or pointers) from a thread-safe queue structure where you store everything that needs to be done.

This way you avoid the overhead from creating lots of threads, and still maintain the same amount of concurrency.

Upvotes: 3

Related Questions