tags:

views:

98

answers:

1

How do you run several pthreads, in C, and detect the first to terminate?

I'm thinking there has got to be an interface similar to select() for sockets to do this with threads.

Thanks, Chenz

+2  A: 
  1. Use a shared message queue (with a mutex/condition) and have each thread post a message when they are finished.

  2. Have a shared variable protected by a mutex, have the thread write its pthread id

In all cases, you can have a condition in order to wait effectively without too much polling.

jldupont
Actually that doesn't tell you the thread is finished. It only tells you it's nearly finished. But I can't currently think of reason why you'd need to know that one of a group of threads has terminated, as opposed to knowing that one of them has done the thing you really care about. So it may answer the real problem if not the question.
Steve Jessop
That's what I inferred to. Thanks Steve.
jldupont