acces aux attributs dans methode threader
Bonjour
j'ai un probleme avec mes attributs
je crée un thread pour mon client
et dans ma méthode run je n'accedes plus à mes attributs
en fait je change le rep(booleen) quand j'ai un message, je le mets a true
et dans le run je teste ce booleen
mais dans le run il change pas
et la meme chose pour mon tampon de message
mes attributs sont en private dans mon en tete(.h)
d'avance merci pour tout conseil
Classe Client.cpp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
//Surcharge du constructeur
void Client :: initSock(int csock){
sock=csock;
std::cout<<"Client :: ajout 1 client : "<<sock<<std::endl;
//Lancement du Thread
pthread_t unThread;
if((idThread = pthread_create(&unThread,NULL,threadClient,this))== -1){
perror("Error :: create thread failure ");
exit(0);
}
}void Client :: run(){
rep=false;
while(1){
if(rep){
if((send(sock,msgTampon.c_str(),strlen(msgTampon.c_str())+1,0))== -1){
perror("Error : send ");
}
rep=false;
}
}
}
//Envoyer messge au client
void Client :: sendMsg(std::string msg){
msgTampon=msg;
std::cout<<"met dans tampon "<<msgTampon<<std::endl;
rep=true;
}
//Creation de thread pour chaque client
void * threadClient(void * zz){
Client * p=(Client *)zz;
p->run();
} |