多线程之pthread_detach和 pthread_join
2014-10-24 16:05
204 查看
每个进程创建以后都应该调用pthread_join 或 pthread_detach 函数,只有这样在线程结束的时候资源(线程的描述信息和stack)才能被释放.
因此,Pthread创建线程后必须使用join或detach释放线程资源
man pthread_detach:
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.)<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19.2000007629395px; background-color: rgb(255, 255, 255);"> </span>
查看 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会导致内存泄漏, 如果你创建的线程越多,你的内存利用率就会越高, 直到你再无法创建线程,最终只能结束进程。
解决方式有3种,如下:
1)线程里面调用 pthread_detach(pthread_self())
代码如下:
2)在创建线程时,设置线程的PTHREAD_CREATE_DETACHED属性
代码如下:
3)创建线程后用 pthread_join() 一直等待子线程结束
这种方式会阻塞主线程。
代码如下:
因此,Pthread创建线程后必须使用join或detach释放线程资源
man pthread_detach:
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.)<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; line-height: 19.2000007629395px; background-color: rgb(255, 255, 255);"> </span>
查看 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会导致内存泄漏, 如果你创建的线程越多,你的内存利用率就会越高, 直到你再无法创建线程,最终只能结束进程。
解决方式有3种,如下:
1)线程里面调用 pthread_detach(pthread_self())
代码如下:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *PrintHello(void) { pthread_detach(pthread_self()); int stack[1024 * 20] = {0,}; long tid = 0; } int main (int argc, char *argv[]) { pthread_t pid; int rc; long t; while (1) { printf(“In main: creating thread %ld\n”, t); rc = pthread_create(&pid, NULL, PrintHello, NULL); if (rc) { printf(“ERROR; return code from pthread_create() is %d\n”, rc); } sleep(1); } printf(” \n— main End —- \n”); pthread_exit(NULL); }
2)在创建线程时,设置线程的PTHREAD_CREATE_DETACHED属性
代码如下:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *PrintHello(void) { int stack[1024 * 20] = {0,}; } int main (int argc, char *argv[]) { pthread_t pid; int rc; long t; while (1) { printf(“In main: creating thread %ld\n”, t); pthread_attr_t attr; pthread_t thread; pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); rc = pthread_create(&pid, &attr, PrintHello, NULL); pthread_attr_destroy (&attr); if (rc) { printf(“ERROR; return code from pthread_create() is %d\n”, rc); } sleep(1); } printf(” \n— main End —- \n”); pthread_exit(NULL); }
3)创建线程后用 pthread_join() 一直等待子线程结束
这种方式会阻塞主线程。
代码如下:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> void *PrintHello(void) { int stack[1024 * 20] = {0,}; long tid = 0; } int main (int argc, char *argv[]) { pthread_t pid; int rc; long t; while (1) { printf(“In main: creating thread %ld\n”, t); rc = pthread_create(&pid, NULL, PrintHello, NULL); if (rc) { printf(“ERROR; return code from pthread_create() is %d\n”, rc); } pthread_join(pid, NULL); sleep(1); } printf(” \n— main End —- \n”); pthread_exit(NULL); }
相关文章推荐
- 多线程函数系列pthread_create(), pthread_join(), pthread_self(),pthread_exit(), pthread_detach()实例详解
- c/c++ linux c 多线程 pthread_detach(id); phthread_join(id,0);
- pthread_join和pthread_detach详解
- pthread_join pthread_exit pthread_detach
- pthread_join与pthread_detach详解(转)
- linux下多线程 之 pthread_detach(pthread_self())
- 如果你不想让pthread_join阻塞你的进程,那么请调用pthread_detach
- pthread_join和pthread_detach详解
- Pthread创建线程后必须使用join或detach释放线程资源
- 解决undefined reference to `pthread_atfork'/`pthread_detach'/`pthread_create'/`pthread_join'
- Pthread创建线程后必须使用join或detach释放线程资源
- call pthread_join() or pthread_detach() for every thread
- pthread join 和detach 的处理
- linux线程之pthread_join和pthread_detach
- linux下多线程之pthread_detach(pthread_self())
- pthread_join和pthread_detach
- pthread_detach pthread_join pthread_create
- 线程原语:pthread_create(),pthread_self(),pthread_exit(),pthread_join(),pthread_cancel(),pthread_detach(
- pthread_join与pthread_detach细节问题
- 多线程编程 c++ /thread(detach,join)/ _beginthreadex