如何检查如果一个线程使用的pthread时终止? [英] How do I check if a thread is terminated when using pthread?

查看:132
本文介绍了如何检查如果一个线程使用的pthread时终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查如果一个线程被终止?就我而言,我已经my_pthread [5],我想检查是否有5个线程的完成自己的工作(被终止? - 我不知道),那么我可以给他们更多的工作要做。

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

如果我用在pthread_join(),那么它必须是:

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

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

如果线程[3]线程[0]之前完成,然后我不得不等待thread0,1,2,完成什么?这不是我想要的。

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

推荐答案

这听起来像你想要的东西是不是等待一个线程完成/退出,但对于一个线程
信号,即它与完成工作,这样你就可以给他们更多的工作。

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.

我会做的就是


  • 创建5个工作队列和1的工作结果队列

  • create 5 work queues, and 1 work result queue

5线程环路从其工作队列和岗位业绩获取工作回到相同的结果队列。

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

(发送工作到5个线程)的主线程会做这样的事情:

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);
}

每个5个工作线程(处理一些工作,张贴结果返回给主线程)会做的:

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);
}

在code来实现这样的队列中的位置:
http://asgaard.homelinux.org/svn/threadqueue/

这篇关于如何检查如果一个线程使用的pthread时终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆