views:

87

answers:

1

I know I can call thread.join() to force a thread to complete before the current thread can proceed. However, my program has a bunch of files that are read into memory, modified, and then flushed to disk. Each flush is being done in a separate thread so that the current thread can continue while the contents are flushed to disk.

I could keep a set of all spawned threads and then join them all at the end of the main thread's execution, but if the program runs for a long time there could be a huge number of flush-threads, most of which completed anyway.

Is there any way to join all active threads before proceeding and exiting the main thread?

+4  A: 

If the threads have completes execution they aren't actually threads any more. I don't see your concern. I for one would consider using a thread pool. If you want to keep a list of all spawned threads use boost::thread_group. And joining all threads is essentially and effectively the same as joining all active threads since join on a Not-A-Thread returns immediately. HTH

Armen Tsirunyan