- 论坛徽章:
- 0
|
测试系统是CentOS-7-x86_64-DVD-1511
代码如下:- 1 #include <stdio.h>
- 2 #include <pthread.h>
- 3 #include <errno.h>
- 4 extern int errno;
- 5 void* foo(void* x)
- 6 { sleep(3600); }
- 7 int main()
- 8 {
- 9 int i = 0,err;
- 10 pthread_t thread;
- 11 pthread_attr_t attr;
- 12 pthread_attr_init(&attr);
- 13 pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
- 14 //pthread_attr_setstacksize(&attr,50*1024);
- 15 while (++i) {
- 16 if ((err=pthread_create(&thread, &attr, foo, NULL)) != 0) {
- 17 printf("threadnum=%d ,err=%d, errno=%d\n",i-1,err,errno);
- 18 return 1;
- 19 }
- 20 }
- 21 }
复制代码 当前环境为:
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 3797
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 3797
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
程序运行结果为:
$ maxthread
threadnum=3640 ,err=11, errno=11
缩小线程栈空间
ulimit -s 1000
再次运行程序:
maxthread
threadnum=3640 ,err=11, errno=11
结果依然相同。
通过代码方式修改栈空间:
把第14行注释取消,设置50k的空间,重编译后,再跑
$ maxthread
threadnum=3640 ,err=11, errno=11
结果还是相同。
我比较奇怪,3640是最大的单进程线程数吗?不管线程栈空间怎么改变,线程数量总是这么多个。
请教了! |
|