views:

193

answers:

5
+1  Q: 

Pthreads question

How to check if a thread is terminated? In my case, I have my_pthread[5] and I want to check if any in 5 threads has finished its job (terminated???I'm not sure) then I can give them another work to do.

If I use pthread_join(), then it has to be

pthread_join(my_pthread[0]);
...
pthread_join(my_pthread[4]);

What if thread[3] finishes before thread[0] and then I have to wait thread0, 1, 2 finish?? Not what I want.

+1  A: 
  1. If they aren't detached, you could wait on them using pthread_join: this method "waits" for a thread to finish. I am not sure that's what you want.

  2. If you just want to assign them some other job (i.e. keep them running), then use a communication channel of some sort e.g. a thread-safe queue. In other words, use a queue to signal when the job is done and push new jobs through a separate queue.

Also, have a look at this thread on SO when it comes to detached thread.

jldupont
A: 

I know that if you are using the wait() function for your parent function, there are signals that are set for the child threads. You can then use some macro's that are defined in wait.h that will let you know what the status of the thread that are finished are. Additionally, if the thread is done and you use the exit() command, what ever the parameter you set there is sent back to the parent thread. Mind you this is all in the C world. Other languages' mileage may vary.

Ron Hunt
A: 

Been a long time since I've worked with pthreads specifically, but with thread programming in general you wait for the thread to complete with a call to join(). Once the call to join completes you know that thread has completed its work.

Chris
+2  A: 

It sounds like what you want is not to wait for a thread to finish/exit, but for a thread to signal that it's done with work, so you can feed them more work.

What I would do is

  • create 5 work queues, and 1 work result queue

  • The 5 threads loops by fetching work from its work queue and posts results back to the same result queue.

The main thread(sending work to the 5 threads) would do something like this:

for(;;) {
  struct threadmessage msg;
  struct *work_result;
  struct *work;
  thread_queue_get(&result_queue,NULL,&msg);
  work_result = msg->data;
  handle_result(work_result);
  work = get_more_work();
  thread_queue_add(worK_result->queue,work,0); 
  free_work_result(work_result);
}

Each of the 5 worker threads (processing some work, posting the result back to the main thread) would do:

for(;;) {
  struct threadmessage msg;
  struct *work_result;
  struct *work;
  thread_queue_get(my_queue,NULL,&msg);
  work = msg->data;
  process(work_result);
  work_result->queue = my_queue;
  thread_queue_add(&result_queue,work_result,0);
  free_work(work);
}

The code to implement such a queue are here: http://asgaard.homelinux.org/svn/threadqueue/

nos
A: 

If I'm right you want to know which of your 5 threads finishes his job first. May be it's a good idea to use a semaphore for each of them to signal to the main thread that they are done. Like this:

sem_t signal;

void *working_thread(void *p)
{
   // do somthing useful
  sem_post(&signal);

}

int main()
{
  // create 5 threads and semaphore
  sem_wait(&signal);
  return 0;
}
Nikita Borodulin