comment utiliser pthread_exit et pthread_join
Bonjour,
mon programme principale doit attendre mon thread avant de se terminer.
Mon problème : est ce que l'appel à pthread_exit( ) et à pthread_join( ) est correcte ? il me semble pas correcte car mon programme tourne en boucle ( ne se termine pas ) après avoir fait les printf
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 41 42 43 44 45 46 47 48 49
|
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
char theChar = '\0';
unsigned int C_mode = 0;
unsigned int L_mode = 0;
int the_status = 1;
void* ask_for_login(void* name) {
while((L_mode == 0) && (C_mode == 0)){
printf("ask_for_login...\n");
theChar = getchar();
L_mode = 1;
printf("vous avez entre votre login : L <-> %d\n", theChar);
}
pthread_exit(the_status);
return NULL;
}
int main (void) {
pthread_t thread_L;
void* status;
printf("veuillez inserer votre carte ou entrer votre login\n");
if (pthread_create(&thread_L, NULL, ask_for_login, "BB")) {
perror("pthread_create");
printf("erreur : pthread_create\n");
exit(EXIT_FAILURE);
}
printf("avant sleep\n");
sleep(5);
printf("apres sleep\n");
C_mode = 1;
printf("carte inseree\n");
if (pthread_join(thread_L,(void *) &status)){
perror("pthread_join");
printf("erreur : pthread_join a echoue\n");
}
printf("Authentification terminee\n") ;
return (EXIT_SUCCESS);
} |
le resultat donne :
Code:
1 2 3 4 5 6 7
|
veuillez inserer votre carte ou entrer votre login
avant sleep
ask_for_login...
apres sleep
carte inseree
// puis attend indéfiniment ... |