至于pthread_create(3)手册页说:
“在Linux/X86-32,对于一个新的线程默认堆栈大小是2兆字节”,除非RLIMIT_STACK
资源限制(ulimit -s
)设置:在在这种情况下,“它确定了新线程的默认堆栈大小”。
您可以通过getrlimit(2)检索RLIMIT_STACK的当前值检查这一事实,如下面的程序:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
int main()
{
/* Warning: error checking removed to keep the example small */
pthread_attr_t attr;
size_t stacksize;
struct rlimit rlim;
pthread_attr_init(&attr);
pthread_attr_getstacksize(&attr, &stacksize);
getrlimit(RLIMIT_STACK, &rlim);
/* Don't know the exact type of rlim_t, but surely it will
fit into a size_t variable. */
printf("%zd\n", (size_t) rlim.rlim_cur);
printf("%zd\n", stacksize);
pthread_attr_destroy(&attr);
return 0;
}
这些结果时试图运行它的命令行(编译a.out
) :
$ ulimit -s
8192
$ ./a.out
8388608
8388608
$ ulimit -s unlimited
$ ./a.out
-1
2097152
$ ulimit -s 4096
$ ./a.out
4194304
4194304
除此之外,linux在需要时会自动生成堆栈 - 但是您的目标受限于这些限制,以及可扩展区域中可用地址空间的限制。 – nos 2010-02-26 08:56:05
nos,仅适用于主线程,不是吗? – osgx 2011-08-25 19:43:18