pthread_create()创建线程最大个数
2009-02-24 11:40
369 查看
线程应用程序最常见导致创建线程失败的原因是线程栈大小的设置。创建一个新的线程,默认情况下系统为线程栈预留了2MB的寻址空间。线程栈起始于进程虚拟 内存的高端地址,并向虚拟内存底端地址方向扩展。取决于线程本身的大小以及其它线程内存分配的情况,进程虚拟地址空间消耗过快可能导致创建线程失败。这里有一个测试程序可以看到,Linux下最多可以创建多少个线程。#include <pthread.h>#include <stdio.h>#include <string.h> void *ThreadFunc(){ static int count = 1; printf ("Create thread%d/n", count); pthread_detach(pthread_self()); count++; } main(void){ int err; pthread_t tid; while (1){ err= pthread_create(&tid, NULL, ThreadFunc, NULL); if(err != 0){ printf("can't create thread: %s/n",strerror(err)); break; } }}
输出结果如下:Create thread 301Create thread 302can't create thread: Cannot allocate memory用 ulimit -s 可以查看到栈的默认大小为10240K32位linux下的进程用户空间是3072M, 3072/10.24=300。为什么实际会比计算出来的多2个,这个原因还不太清楚。(编者注:准确算法是(3072*1024K)/10240K=307.2,实际会比计算出来的少5.2个)可以在调用pthread_create 的时候用 pthread_attr_getstacksize 设置栈的大小,或者直接用 ulimit -s 设置栈的大小。如果修改上面的测试代码为#include <pthread.h>#include <stdio.h>#include <string.h> void *ThreadFunc(){ static int count = 1; printf ("Create thread%d/n", count); pthread_detach(pthread_self()); count++; } main(void){ int err; pthread_t tid; while (1){ err= pthread_create(&tid, NULL, ThreadFunc, NULL); if(err != 0){ printf("can'tcreate thread: %s/n", strerror(err)); break; } }}那么得到的结果将是:Create thread 560000Create thread 560001……………这里用到了pthread_detach(pthread_self())来释放线程所占用的内存资源(线程内核对象和线程堆栈)。这样就可以创建更多的线程,而不会出现Cannot allocate memory的错误。如果进程中的某个线程执行了pthread_detach(th),则th线程将处于DETACHED状态,这使得th线程在结束运行时自行释放所占用的内存资源。一个可join的线程所占用的内存仅当有线程对其执行了pthread_join()后才会释放,因此为了避免内存泄漏,所有线程的终止,要么已设为DETACHED,要么就需要使用pthread_join()来回收。
输出结果如下:Create thread 301Create thread 302can't create thread: Cannot allocate memory用 ulimit -s 可以查看到栈的默认大小为10240K32位linux下的进程用户空间是3072M, 3072/10.24=300。为什么实际会比计算出来的多2个,这个原因还不太清楚。(编者注:准确算法是(3072*1024K)/10240K=307.2,实际会比计算出来的少5.2个)可以在调用pthread_create 的时候用 pthread_attr_getstacksize 设置栈的大小,或者直接用 ulimit -s 设置栈的大小。如果修改上面的测试代码为#include <pthread.h>#include <stdio.h>#include <string.h> void *ThreadFunc(){ static int count = 1; printf ("Create thread%d/n", count); pthread_detach(pthread_self()); count++; } main(void){ int err; pthread_t tid; while (1){ err= pthread_create(&tid, NULL, ThreadFunc, NULL); if(err != 0){ printf("can'tcreate thread: %s/n", strerror(err)); break; } }}那么得到的结果将是:Create thread 560000Create thread 560001……………这里用到了pthread_detach(pthread_self())来释放线程所占用的内存资源(线程内核对象和线程堆栈)。这样就可以创建更多的线程,而不会出现Cannot allocate memory的错误。如果进程中的某个线程执行了pthread_detach(th),则th线程将处于DETACHED状态,这使得th线程在结束运行时自行释放所占用的内存资源。一个可join的线程所占用的内存仅当有线程对其执行了pthread_join()后才会释放,因此为了避免内存泄漏,所有线程的终止,要么已设为DETACHED,要么就需要使用pthread_join()来回收。
相关文章推荐
- pthread_create()创建线程最大个数
- pthread_create()创建线程最大个数
- pthread_create()创建线程时传入多个参数
- C++ pthread_create 线程创建与传参(struct)
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- pthread_create线程创建的过程剖析
- c与c++在使用pthread_create创建线程时的区别
- linux 创建线程问题undefined reference to 'pthread_create'
- 创建线程:pthread_create
- pthread_key_create()--创建线程私有数据|pthread_key_delete()--注销线程私有数据
- 将创建线程的API-pthread_create封装成一个线程类
- pthread_create线程创建的过程剖析
- 对线程创建函数pthread_create的总结
- pthread_create线程创建的过程剖析
- 线程创建 pthread_create 中自定义参数注意事项
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- pthread_create线程创建的过程剖析 http://blog.csdn.net/yetyongjin/article/details/7673837
- linux 线程创建函数pthread_create的几个传参方式
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- 使用pthread_create()创建线程