J'utilise MINGW dans mon ordinateur Windows 7 pour le codage de thread POSIX.Les 2 premiers threads ne sont pas exécutés si je crée des threads dans une boucle
Tenir compte du code simple suivant:
#include <stdio.h>
#include <pthread.h>
#include <process.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello Dude...!!!\t I am thread no #%ld\n",tid);
pthread_exit(NULL);
}
int main()
{
pthread_t thread[NUM_THREADS];
int rc;
long t;
for(t=0;t<NUM_THREADS;t++)
{
printf("Inside the Main Thread...\nSpawning Threads...\n");
rc=pthread_create(&thread[t],NULL,PrintHello,(void*)t);
if(rc)
{
printf("ERROR: Thread Spawning returned code %d\n",rc);
exit(-1);
}
}
return 0;
}
programme ci-dessus lorsqu'il est exécuté dans mon système, il montre la sortie suivante:
Inside the Main Thread...
Spawning Threads...
Inside the Main Thread...
Spawning Threads...
Hello Dude...!!! I am thread no #0
Inside the Main Thread...
Spawning Threads...
Hello Dude...!!! I am thread no #1
Inside the Main Thread...
Spawning Threads...
Hello Dude...!!! I am thread no #2
Inside the Main Thread...
Spawning Threads...
Ce programme était censé engendrer 5 fils. Mais il avait créé seulement 2 threads. La première ligne 2 et la dernière 2 suggère que la routine pthread_create() est sur le point d'être appelée. Et puisque la variable "rc" n'est pas "1", il n'y a aucune erreur dans la création du thread, sinon elle aurait atteint la partie "if (rc)".
Alors où est l'erreur? Ou c'est quelque chose lié à ma machine Windows.
J'avais changé le long à int mais encore le premier thread ne se fait pas exécuter ... – Shantanu
Merci cela a fonctionné ... – Shantanu