首页 > 代码库 > c++ thread

c++ thread

Either pthread_join(3) 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_join 或 pthread_detach.但是再查了 Pthread for win32 pthread_join
When a joinable thread terminates, its memory resources (thread descriptor and stack) are not deallocated until another thread performs pthread_join on it. Therefore, pthread_join must be called once for each joinable thread created to avoid memory leaks.

才知道如果在新线程里面没有调用pthread_join 或 pthread_detach会导致内存泄漏, 如果你创建的线程越多,你的内存利用率就会越高, 直到你再无法创建线程,最终只能结束进程。

 1.在创建thread时候线程就开始执行了,而为什么join与detach原因如上

c++ thread