是否有必要调用在pthread_join() [英] is it necessary to call pthread_join()

查看:200
本文介绍了是否有必要调用在pthread_join()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建我的主要超过100个线程(),所以我只是想知道,我是否需要调用在pthread_join()之前,我离开我的主要()。
此外,我并不需要这些线程生成的数据,基本上所有的线程正在做一些工作独立于main()和其他线程。

I create more than 100 thread from my main() so I just wanted to know that do i need to call pthread_join() before i exit my main(). Also I do not need the data generated by these threads, basically all the threads are doing some job independent from main() and other threads.

推荐答案

在pthread_join 做了两件事:


  1. 等待线程完成。

  2. 清理与线程关联的任何资源。

如果你退出的过程中不加入,那么(2)会为你的操作系统来完成(尽管它不会做线程取消清理,只是核弹从轨道线),以及(1)不会。因此,无论您需要调用在pthread_join 取决于你是否需要(1)发生的。

If you exit the process without joining, then (2) will be done for you by the OS (although it won't do thread cancellation cleanup, just nuke the thread from orbit), and (1) will not. So whether you need to call pthread_join depends whether you need (1) to happen.

如果因为其他人都在说,你不妨取下它,你不需要的线程来运行,那么。分离线程不能加入(所以你不能等待其完成),但其释放资源,如果自动的确已完成。

If you don't need the thread to run, then as everyone else is saying you may as well detach it. A detached thread cannot be joined (so you can't wait on its completion), but its resources are freed automatically if it does complete.

这篇关于是否有必要调用在pthread_join()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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