Les threads

hwida12 Messages postés 18 Statut Membre -  
loupius Messages postés 789 Statut Membre -
Bonjour, je suis entrain de faire un programme manipulant l'utilisation des threads.
J'ai créé deux threads et leurs fonctions auxquelles ils feront appel.Le problème que j'ai, c'est lors de l'appel de pthread_create( , , ,) me génère une erreur au niveau du 3ème paramètre de la fonction appelée.
Voici l'erreur:
houda@houda-laptop:~/Bureau$ gcc exp.c -o thread -lpthread
exp.c: In function ‘main’:
exp.c:48: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
exp.c:50: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
Voici le programme:

/////////////////////////
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<sys/types.h>
pthread_cond_t est_libre;
pthread_cond_t est_plein;
pthread_mutex_t protect;
char *buffer;

void deposer(const char *msg)
{
pthread_mutex_lock(&protect);
while(buffer!=NULL)
{
pthread_cond_wait(&est_libre,&protect);
}
buffer=strdup(msg);
pthread_cond_signal(&est_plein);
pthread_mutex_unlock(&protect);
}

char *retirer(void)
{
char *result;
pthread_mutex_lock(&protect);
while(buffer==NULL)
{
pthread_cond_wait(&est_plein,&protect);
}
result=buffer;
buffer=NULL;
pthread_cond_signal(&est_libre);
pthread_mutex_unlock(&protect);
return result;
}
void init_prodco(void)
{
pthread_mutex_init(&protect,NULL);
pthread_cond_init(&est_libre,NULL);
pthread_cond_init(&est_plein,NULL);
buffer=NULL;
}
int main(void)
{
pthread_t t1,t2;
pthread_create(&t1,NULL,deposer,NULL);
pthread_join(t1,NULL);
pthread_create(&t2,NULL,retirer,NULL);
pthread_join(t1,NULL);

return 0;
}
Merci.
A voir également:

1 réponse

loupius Messages postés 789 Statut Membre 148
 
exp.c:48: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
Comme toujours le compilateur est très explicite. Comme tu ne comprends pas l'anglais, détaillons:
Dans le fichier 'exp.c' à la ligne 48, le 3ème argument passé à la fonction ‘pthread_create’ n'est pas compatible avec le type attendu.
Alors qu'attend le compilateur pour ce 3ème argument:
void*(*fonction)(void* argument)
Alors il faut lui donner une fonction qui attend 1 paramètre de type 'void*' et qui retourne un 'void*'. C'est pas plus compliqué que cela.
Bonne continuation.
Nota: Inutile de reposter ta question, il faut avoir un peu de patience. J'ai demandé la suppression du doublon.
0