How to check if thread terminated using pthread?

How to check if a thread is completed? In my case, I have my_pthread [5], and I want to check if any of the 5 threads has completed its task (completed? - I'm not sure), then I can give them more work.

If I use pthread_join (), it should be:

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

What if thread [3] ends before thread [0], and then I need to wait until thread0, 1, 2 is finished? This is not what I want.

+1
source share
5 answers

It looks like you do not want to wait for the end or exit of the stream, but for the stream, the signal that it did with the work, so that you can feed them more work.

What would i do

  • create 5 work queues and 1 work results queue

  • 5 cycles of threads, getting work from the work queue, and messages return to the same result queue.

The main thread (sending work to 5 threads) will 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 workflows (processing some work, sending the result back to the main thread) will perform:

 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 for implementing such a queue is here: http://asgaard.homelinux.org/svn/threadqueue/

+2
source
  • If they are not disconnected, you can wait for them with pthread_join : this method "waits" to complete the thread. I'm not sure what you want.

  • If you just want to assign them some other task (i.e. save them), then use a communication channel of some type, for example. thread safe queue. In other words, use the queue to signal when a task is complete and click new tasks through a separate queue.

Also, look at thread on SO when it comes to dedicated thread.

+1
source

I know that if you use the wait () function for your parent function, there are signals that are set for child threads. Then you can use the macro defined in wait.h, which will let you know what the status of the completed stream is. In addition, if the stream is executed and you use the exit () command, then whatever parameter you set there is not sent back to the parent stream. Keep in mind that this is all in world C. Omission of other languages ​​may vary.

0
source

It has been a long time since I worked with pthreads specifically, but with thread programming in general, you are waiting for the thread to finish with a call to join (). Once the join call is complete, you know that the thread has completed its work.

0
source

If I'm right, you want to know which of your 5 threads is finishing its work. It may be 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; } 
0
source

All Articles