实现 pthread 池
我需要使用 pthread 实现线程池。我无法前进。我在此处发现了类似的问题 但这仍然不能澄清我的问题。我的问题是,一旦线程运行到终止状态,我可以再次使用它吗?提出这个问题的另一种方式是,线程在运行任务完成后如何返回其线程池。有人能给我一些简单的 pthread 池文章吗?我的困惑主要是因为我有一点java背景。我在某处读到,一旦线程终止,我们就无法在线程上第二次调用 start() 。
I need to implement a thread pool using pthreads. I could not move forward. I found similar question here
But that still does not clarify my question. My question is once a thread runs to its termination can I use it again? Another way of putting this question is, How does a thread return to its thread pool after running a task to its completion. Could anyone point me to some simple pthread pool article? My confusion arises mainly because I have little bit of java background. I read somewhere we cannot call start() on thread second time once it terminates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这就是池的目的,重用线程而不是销毁它们。
通过尝试从队列中获取另一个元素。循环执行是一种方法。
以下是我的实现中每个线程所做的事情(这是与 pthread_create 一起使用的实际函数):
Yes, that's the purpose of the pool, to reuse threads instead of destroying them.
By trying to get another element from the queue. Doing it in a loop is one way.
Here is what every thread does in my implementation (this is the actual function used with
pthread_create
):