Boost logo

Boost Users :

From: bill_kempf (williamkempf_at_[hidden])
Date: 2002-06-10 15:55:50

--- In Boost-Users_at_y..., "rgalek2002" <rfox_at_s...> wrote:
> Hi,
> I have an application which starts a number of threads, and I want
> the application to wait for the threads to finish before exiting.
> Standard stuff, I know, but never having used threads before I am
> wondering what the stadard way is to accomplish this.
> I have (psuedo code)
> for (each record in database) {
> thread thrd(func(), data);
> }
> I could imagine storing all my thread objects in a list, and then
> calling join() on each one. For example something like
> list <thread> threads;
> for (each rec in db) {
> thread thrd(func(), data);
> threads.insert(threads.begin(),thrd);
> }
> list<thread>::iterator i;
> for (i=threads.begin(); i!=threads.end(); i++) {
> thread t = *i;
> t.join();
> }
> Does this seem reasonable? Is there a better/standard way?

This is precisely what boost::thread_group was designed for, and it
works very similar to your pseudo code.

> One thing
> I don't like here is that each join will block, when what I would
> like to do is something more like join_all();

There's no "end result" difference between calling join() for each
individual thread and calling some "join_all()" method. On platforms
like Win32 there might be some subtle performance differences, but
probably not enough to be worth considering.

Bill Kempf

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at