linux创建线程失败的原因是线程栈大小的设置
2014-03-14 16:09
387 查看
线程应用程序最常见导致创建线程失败的原因是线程栈大小的设置。创建一个新的线程,默认情况下系统为线程栈预留了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 301
Create thread 302
can't create thread: Cannot allocate memory
用 ulimit -s 可以查看到栈的默认大小为10240K
32位linux下的进程用户空间是3072M, 3072/10.24=300。为什么实际会比计算出来的多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't create thread: %s\n", strerror(err));
break;
}
}
}
那么得到的结果将是:
Create thread 560000
Create thread 560001
……………
这里用到了pthread_detach (pthread_self())来释放线程所占用的内存资源(线程内核对象和线程堆栈)。这样就可以创建更多的线程,而不会出现Cannot allocate memory的错误。
这里有一个测试程序可以看到,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 301
Create thread 302
can't create thread: Cannot allocate memory
用 ulimit -s 可以查看到栈的默认大小为10240K
32位linux下的进程用户空间是3072M, 3072/10.24=300。为什么实际会比计算出来的多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't create thread: %s\n", strerror(err));
break;
}
}
}
那么得到的结果将是:
Create thread 560000
Create thread 560001
……………
这里用到了pthread_detach (pthread_self())来释放线程所占用的内存资源(线程内核对象和线程堆栈)。这样就可以创建更多的线程,而不会出现Cannot allocate memory的错误。
相关文章推荐
- Linux创建线程的属性设置
- wdcp/wdlinux 在 UBUNTU/linux 中安装失败原因之创建用户
- poll and epoll的区别-linux C线程间通信:阻塞-唤醒-创建文件,重启后大小是0
- linux下线程创建失败问题
- linux线程创建、取消、属性设置
- 线程创建时指定线程栈的大小
- 关于linux动态库中无法设置线程栈大小的问题
- pthread_create函数创建线程失败的原因
- linux下线程的创建,转帖
- ADPLUS使用配置文件设置断点时无法创建DUMP文件原因分析
- Linux 通过lseek()来实现文件大小的设置
- 在设置iis windows身份验证,出错:登录失败:用户帐户限制。可能的原因包括不允许空密码登录时间限制或强制的策略限制。
- linux 线程编程:线程创建
- linux多线程创建失败,errno=1:Operation not permitted
- centos7 sftp设置后 ssh 启动失败 原因分析
- linux下使用crontab实现定时PHP计划任务失败的原因分析
- Linux系统下C语言编程:线程的创建和使用
- linux下C语言多线程(一)线程的创建与取消
- 【Linux】利用fork()创建多个线程
- Linux 系统更新命令行模式,出错原因,软件更新器更新失败,系统提示已为最新版本,问题如何解决