Either pthread_join() or pthread_detach() should be called for each thread that an application creates, so that system resources for the thread can be released. (But note that the resources of all threads are freed when the process terminates.
也就是说:每个进程创建以后都应该调用pthread_join 或 pthread_detach 函数,只有这样在线程结束的时候资源(线程的描述信息和stack)才能被释放.
如果在新线程里面没有调用pthread_join 或 pthread_detach会导致内存泄漏, 如果你创建的线程越多,你的内存利用率就会越高, 直到你再无法创建线程,最终只能结束进程。
解决方

在Python中,为了释放线程资源,应当确保每个线程调用了pthread_join()或pthread_detach()。不调用会导致内存泄漏,影响系统性能。解决方法包括:1) 线程内部调用pthread_detach(pthread_self());2) 创建线程时设置PTHREAD_CREATE_DETACHED属性;3) 使用pthread_join()等待子线程结束。示例代码展示了这三种方法。

最低0.47元/天 解锁文章
2124

被折叠的 条评论
为什么被折叠?



