I have several pthread
children created through pthread_create
and marked as joinable. I may wait for them one after another using pthread_join
, but, I would like to wait for joining anyone (i.e. as soon as any one of them ends).
How do I know which child pthread
has terminated?
Is polling through them one after another through pthread_join
the only solution?
Asked
Active
Viewed 78 times
0

Dr. Debasish Jana
- 6,980
- 4
- 30
- 69
-
2Just code what you want. If you want to know when a thread terminates, code it to tell you before/when it terminates. Or have one thread to block in `pthread_join` for each thread you want to wait for and have that thread tell you when the other thread terminated using any method you want. Just code exactly what you want. – David Schwartz Apr 13 '17 at 07:18
-
@DavidSchwartz Appreciate your thoughts. Instead of using wait notify, I could wait for them one after another using pthread_join, but if there is a direct API that would have helped me with the child thread # that got done, parent thread could have done some other task meanwhile – Dr. Debasish Jana Apr 13 '17 at 07:24
-
1I think this is an [XY question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Can you elaborate on why you wnat to do that? – StoryTeller - Unslander Monica Apr 13 '17 at 07:26
-
@StoryTeller, In Solaris, I have a call thr_join that could let me know which child thread got terminated. I am trying to find out a posix counterpart or alternate to be more platform agnostic – Dr. Debasish Jana Apr 13 '17 at 07:36
-
@Aconcagua yes plus while some child thread is still running, the main thread could do something else and come back to find out who next terminated – Dr. Debasish Jana Apr 13 '17 at 07:45
-
@Dr.DebasishJana Hm, seems as if I deleted my comment too quickly... Now digging deeper (to avoid xy question): Why do you want to do that? Why do you have to know? Several ideas in mind, but each one fits to a different problem... – Aconcagua Apr 13 '17 at 07:54
-
@Aconcagua suppose a thread has spawned N children threads for some tasks, once the parent knows which thread completed the task, it could spawn another one for the next task. of course this could have been done in other ways, but I am constrained with some legacy code – Dr. Debasish Jana Apr 13 '17 at 08:00
-
@Dr.DebasishJana And this legacy code uses pthread_exit? If not, you could create a separate thread function calling the original one within a loop, fetching the next task from some kind of task queue. – Aconcagua Apr 13 '17 at 08:04
-
2@Dr.DebasishJana You don't care whether or not those threads have terminated, you care whether or not they've finished the work they were assigned to do. That the thread terminates when it finishes the work is not a detail that other code should have to care about, and it just makes the problem harder to solve. (If I order a sandwich at a restaurant, I don't care if the employee making my sandwich goes home when they're done making it. I don't really want to know when the employee leaves. I want to know when my sandwich is done. So build a notify scheme around the work.) – David Schwartz Apr 13 '17 at 08:10
-
1But if you must do it this way for some reason, you can wrap the thread start function in a `pthread_cleanup_push`/`pthread_cleanup_pop` pair and pass a pointer to that wrapper function to `pthread_create`. – David Schwartz Apr 13 '17 at 08:14
1 Answers
0
AS per my understanding you need some asynchronous way of knowing when a thread is finished. As many have said in the comments have a notification scheme. I had a similar problem, but my worker threads also needed some communication. I had a domain socket (listen) in main thread, the first thing a new thread does was to connect and send its identification to main thread and the last was to disconnect (close). Meanwhile in the main thread you can use good old select or epoll to know when an fd got closed, based on that you can figure out which thread got closed and what to do.

Nish
- 379
- 2
- 9
-
A much cheaper way of communication would be to use atomic variables or may be a queue (saved by mutex against concurrent access). Or did I oversee something? – Scheff's Cat Apr 13 '17 at 08:37