1. 首先可以运行下面的程序检查,检查你现在的系统总共能创建多少个线程。我的系统上是i=380,也就是最多能创建380个线程。后面分析为什么是它。
// maxthread.cc , compile: gcc maxthread.cc -o thread -lpthread
include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
static void *foo( void * ptr);
int main()
{
int i = 0;
pthread_t thread;
while (1) {
if (pthread_create(&thread, NULL, foo, NULL) != 0)
return 1;
i ++;
printf("i = %d\n", i);
}
return 0;
}
void *foo(void * ptr)
{
printf("Call foo()\n");
}
2. 栈大小的限制,执行下面程序:
int main(int argc, char * argv[])
{
char buf[1024 * 1024 * 9];
buf [1024 * 1024 * 9 - 1] = 'a';
return 0;
}