linux默认进程栈大小,linux线程栈大小的设置

线程应用程序最常见导致创建线程失败的原因是线程栈大小的设置。创建一个新的线程,默认情况下系统为线程栈预留了2MB的寻址空间。线程栈起始于进程虚拟内存的高端地址,并向虚拟内存底端地址方向扩展。取决于线程本身的大小以及其它线程内存分配的情况,进程虚拟地址空间消耗过快可能导致创建线程失败。

这里有一个测试程序可以看到,Linux下最多可以创建多少个线程。

#include

#include

#include

void *ThreadFunc()

{

static int

count = 1;

printf

("Create thread %d/n", count);

//pthread_detach (pthread_self());

count++;

}

int main(char *argv[ ], int argc)

{

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 303

Create thread 30