join我创建的每个线程吗

join我创建的每个线程吗

本文介绍了我必须pthread_join我创建的每个线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pthread_join()手册页中:

joinable线程终止时,直到线程对其执行pthread_join之前,它的内存资源(线程描述符和stack)才被释放.因此,必须为创建的每个joinable线程调用一次pthread_join,以避免内存泄漏.

When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until thread performs pthread_join on it. Therefore, pthread_join must be called once for each joinable thread created to avoid memory leaks.

这是否意味着我需要加入我创建的每个线程以防止泄漏?但是加入会阻止呼叫者.

Does it mean i need to join each thread i create to prevent leaks? But joining blocks the caller.

请解释更多.

推荐答案

您不需要加入,但这是一个好主意.如果不调用pthread_join(),则main()函数可能会在线程终止之前返回.在这种情况下,pthread_join()使应用程序等待,直到另一个线程完成处理.另外,当您加入线程时,它使您有机会检查返回值并确保一切顺利,并且使您有机会清理可能与线程共享的任何资源.

You don't need to join a thread, but it is a good idea. Without calling pthread_join(), there is a possibility that the main() function will return before the thread terminates. In this case, pthread_join() makes the application wait until the other thread finishes processing. Plus, when you join the thread, it gives you the opportunity to check for return values and make sure that everything went smoothly, and it gives you the opportunity to clean up any resources you may have shared with the thread.

您可能感兴趣的一个函数是pthread_detach(). pthread_detach()允许在线程终止后清理线程的存储,因此以后无需再加入线程.

A function that may be of interest to you is pthread_detach(). pthread_detach() allows the thread's storage to be cleaned up after the thread terminates, so there is no need to join the thread afterwards.

这篇关于我必须pthread_join我创建的每个线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:28