How can I wait for any/all pthreads to complete?

The proper way is to keep track of all of your pthread_id's, but you asked for a quick and dirty way so here it is. Basically.

The proper way is to keep track of all of your pthread_id's, but you asked for a quick and dirty way so here it is. Basically: just keep a total count of running threads, increment it in the main loop before calling pthread_create, decrement the thread count as each thread finishes. Then sleep at the end of the main process until the count returns to 0.. Volatile int running_threads = 0; pthread_mutex_t running_mutex = PTHREAD_MUTEX_INITIALIZER; void * threadStart() { // do the thread work pthread_mutex_lock(&running_mutex); running_threads--; pthread_mutex_unlock(&running_mutex); } int main() { for (i = 0; I 0) { sleep(1); } }.

This can be accomplished much more easily with a barrier instead of counter and mutex. – R.. May 27 at 20:11 I like this solution - it hadn't occurred to me simply count the running instances. I think you could even do away with the need for mutexes as the operations are all atomic, IINM.

– SlappyTheFish May 27 at 20:59 1 The operations are definitely not atomic. The mutex is essential. Look up barriers though; they're a lot easier to use and do the counting for you.

– R.. May 27 at 21:54 I just looked it up, yes, it's not atomic. Interesting though - what would be an intermediary state of an incrementation operation? Partially set/unset bits?

Anyway, thanks for the note about barriers - I am investigating them, they sound interesting. – SlappyTheFish May 27 at 22:11 I don't like the solution - because it means I have to keep polling and sleeping - but it still is indeed the simplest! In reality - I would not fall into the while/sleep loop until I received a shutdown signal - so it wouldn't really burn any CPU cycles in the real-world.

– Brad May 277 at 18:29.

If not, you can have your main thread simply call pthread_exit() instead of returning (or calling exit()). If main() returns it implicitly calls (or behaves as if it called) exit(), which will terminate the process. However, if main() calls pthread_exit() instead of returning, that implicit call to exit() doesn't occur and the process won't immediately end - it'll end when all threads have terminated.

pubs.opengroup.org/onlinepubs/007908799/... Can't get too much quick-n-dirtier. Here's a small example program that will let you see the difference. Pass -DUSE_PTHREAD_EXIT to the compiler to see the process wait for all threads to finish.

Compile without that macro defined to see the process stop threads in their tracks. #include #include #include #include static void sleep(int ms) { struct timespec waittime; waittime. Tv_sec = (ms / 1000); ms = ms % 1000; waittime.

Tv_nsec = ms * 1000 * 1000; nanosleep( &waittime, NULL); } void* threadfunc( void* c) { int id = (int) c; int I = 0; for (i = 0 ; I.

Thanks for the reply! Actually - yes - the main thread needs to clean-up/remove a shared memory segment - so I can't just call pthread_exit as you have described. (I now realize I should have stated this in the OP).

Thanks for the response! – Brad May 31 at 18:23.

If you don't want to keep track of your threads then you can detach the threads so you don't have to care about them, but in order to tell when they are finished you will have to go a bit further. One trick would be to keep a list (linked list, array, whatever) of the threads' statuses. When a thread starts it sets its status in the array to something like THREAD_STATUS_RUNNING and just before it ends it updates its status to something like THREAD_STATUS_STOPPED.

Then when you want to check if all threads have stopped you can just iterate over this array and check all the statuses. Don't forget though that if you do something like this, you will need to control access to the array so that only one thread can access (read and write) it at a time, so you'll need to use a mutex on it.

This solution doesn't make things any easier. If you're going to make that ugly array (which you need to synchronize, by the way! ), you could instead just store the pthread_t ids in it and pthread_join them all.

– R.. May 27 at 20:14 1 You're right - but the OP does say that threads come and go and that he doesn't want to keep track of them, which I interpreted as meaning that he doesn't want to join all threads, but simply be able wait for all current threads to end when an exit condition arises. As gravitron suggests, counting running threads would be simpler and avoid synchronisation, but the array approach adds flexibility (should it be required) so that each thread could be described by a struct containing more info such as when it was started, etc to perhaps help with thread monitoring. – SlappyTheFish May 27 at 21:49 Regardless, your approach has serious bugs.

A thread cannot set its own status to THREAD_STATUS_RUNNING because there will be a race condition before it's set. Instead, the creating thread needs to do this before calling pthread_create. There's a lot more synchronization that would need to be done to make your approach valid.

If you're not an expert in this field, pthread_join (or barriers) would be a much simpler and much less error-prone solution. – R.. May 27 at 21:53 Absolutely - again, you're right, and your suggested method of avoiding the race condition is spot on. In fact, that's exactly how I solved it in one of my projects.

I think gravitron or Michael Burr have solutions more in line with what the OP wants, I'm just giving food for thought :-) – SlappyTheFish May 27 at 21:57 I can't just call detach - because I need to do some cleanup in my main thread after all the threads have quiesced and exited. You are correct in that I was hoping for something much simpler than keeping track of (locking, synchronizing) and rejoining all the worker threads. Thanks for the reply!

– Brad May 277 at 18:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions