這是我創建一些線程的代碼。我想在同一時間創建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