如何检查如果一个线程使用的pthread时终止?

编程入门 行业动态 更新时间:2024-10-17 21:15:27
本文介绍了如何检查如果一个线程使用的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来实现这样的队列中的位置: asgaard.homelinux/svn/threadqueue/

更多推荐

如何检查如果一个线程使用的pthread时终止?

本文发布于:2023-10-09 05:50:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   pthread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!