multithreading - pthread third argument in c language -


i'd create threads

int joueur=3;  // in case in "for" loop jouer(joueur); 

i used syntax

i have tried this:

int *joueur = malloc(sizeof(*joueur)); //if (joueur == null)     //donotstartthethread_problemallocatingmemory(); pthread_create(&threads[joueur], null, jouer, (int *) joueur);   jouer function  void jouer(int joueur)  {  while(but_trouve==0)   {   pthread_mutex_lock (&mutex);      for(joueur=0;joueur<nombre_joueurs;joueur++) if(labyrinthe[joueur_ligne[joueur]][(joueur_colonne[joueur])%4]="b") but_trouve=1;      if (but_trouve==1) break; // si un joueur trouve le on termine la partie   deplacer(joueur); //  pthread_cond_signal (&condition); /* on délivre le signal : condition remplie */   pthread_mutex_unlock (&mutex);  // fin de la zone protegee    affiche();    }  pthread_exit(null);  } 

but have message now.

 warning: passing argument 3 of ‘pthread_create’ incompatible pointer type [enabled default]   pthread_create(&threads[threads[nombre_joueurs]], null, jouer, (int *) joueur); in file included /home/nouha/test.c:4:0: /usr/include/pthread.h:244:12: note: expected ‘void * (*)(void *)’ argument of type ‘void (*)(int)’  extern int pthread_create (pthread_t *__restrict __newthread, 

, thank reading,

you passing value of variable, expects address.

you can't pass joueur's address pthread_create() data parameter because it's local variable , deallocated when function returns, might happen before thread finishes working.

i suggest

int *joueur = malloc(sizeof(*joueur)); if (joueur == null)     donotstartthethread_problemallocatingmemory(); pthread_create(&threads[joueur], null, jouer, (void *) joueur); 

note above, type of joueur int *, in example it's int * can't pass pthread_create() function casting void * because it's interpreted address, , doubt 3 valid one.

don't forget free joueur when thread done working poitner, because can't free before or same problem happen.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -