这是我创建一些线程的代码。我想在同一时间创建500个线程,而不是更多。很简单,但是我的代码在创建32xxx线程后失败。pthread_exit(NULL);不工作
然后我不明白为什么我得到32751线程后的错误代码11,因为,每个线程结束。
我可以理解,如果线程不退出,那么在同一台计算机上的32751线程......但在这里,每个线程退出。
这里是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
void *test_tcp(void *);
int main()
{
pthread_t threads[500];
int pointeur_thread;
unsigned long compteur_de_thread=0;
long ttt=0;
int i;
for(int i=0;i<=1000000;i++)
{
ttt++;
pointeur_thread=pthread_create(&threads[compteur_de_thread],NULL,test_tcp,NULL);
if (pointeur_thread!=0)
{
printf("Error : %d\n",pointeur_thread);
exit(0);
}
printf("pointeur_thread : %d - Thread : %ld - Compteur_de_thread : %ld\n",pointeur_thread,compteur_de_thread,ttt);
compteur_de_thread++;
if (compteur_de_thread>=500)
compteur_de_thread=0;
}
printf("The END\n");
}
void *test_tcp(void *thread_arg_void)
{
pthread_exit(NULL);
}
您正在尝试创建一百万个线程。我会拒绝,如果我是你的操作系统... –
可能重复[Pthread \ _create失败后创建几个线程](http://stackoverflow.com/questions/5844428/pthread-create-fails-after-creating-several-线程) –
这会在“同一时间”创建超过500个线程的helluvalot,无论您是否意识到这一点。此外,他们都可以联接,但从未加入。加入他们或创建他们分离。 – WhozCraig