Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
746 connectés 

  FORUM HardWare.fr
  Programmation
  C

  Threads

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Threads

n°2204597
sined40
Posté le 29-09-2013 à 22:32:16  profilanswer
 

Bonjour,
 
J'ai crée un petit programme qui crée 2 threads.
De "façon infinie", les 2 threads font:
 - le 1er thread aprés un délai de x secondes envoie un "signal" pour réveiller le 2e thread.  
 - le 2e thread qui se trouve en attente bloquante sur le signal.
 (enfin je crois:??:)
 
Est-il possible de tuer mon main et de laisser les 2 threads en vie ?
 
PS: Pour info, pour l'instant, je ne vérifie pas les codes de retour.
 
Voici mon bout de code:

Code :
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. pthread_cond_t  Cond_Th01To2;
  5. pthread_mutex_t Mutex_Th01To02;
  6. void* Thread_01_main(void);
  7. void* Thread_02_main(void);
  8. void* Thread_01_main(void)
  9. {
  10.     printf("Execution Thread_01 routine\n" );
  11.    
  12.     while(1)
  13.     {
  14.         printf("Thread_01: Locking the mutex.\n" );
  15.        
  16.         /* Locking the mutex */
  17.         pthread_mutex_lock(&Mutex_Th01To02);
  18.                
  19.         /* Waiting 5 seconds before sending the signal */
  20.         sleep(5);
  21.        
  22.         printf("Thread_01: Sending the signal.\n" );
  23.        
  24.         /* Send a signal to wake up the 2nd Thread */
  25.         pthread_cond_signal(&Cond_Th01To2);
  26.        
  27.         printf("Thread_01: Unlocking the mutex.\n" );
  28.        
  29.         /* Unlock the mutex */
  30.         pthread_mutex_unlock(&Mutex_Th01To02);
  31.     }
  32.     return NULL;
  33. }
  34. void* Thread_02_main(void)
  35. {
  36.     printf("Execution of Thread_02 routine\n" );
  37.    
  38.     while(1)
  39.     {
  40.         printf("Thread_02: Locking the mutex.\n" );
  41.        
  42.         /* Locking the mutex */
  43.         pthread_mutex_lock(&Mutex_Th01To02);
  44.        
  45.         printf("Thread_02: Waiting the signal.\n" );
  46.         /* Waiting the signal to wake up*/
  47.         pthread_cond_wait(&Cond_Th01To2, &Mutex_Th01To02);
  48.        
  49.         /* Receiving the signal */
  50.         printf("Thread_02: Receiving signal.\n" );
  51.        
  52.         /* Unlocking the mutex */
  53.         pthread_mutex_unlock(&Mutex_Th01To02);
  54.        
  55.         /* Receiving the signal */
  56.         printf("Thread_02: Unlocking the mutex.\n" );
  57.     }
  58.    
  59.     return NULL;
  60. }
  61. int main(void)
  62. {
  63.     pthread_t Thread_01;
  64.     pthread_t Thread_02;
  65.     pthread_create(&Thread_01, NULL,(void*) Thread_01_main, NULL);
  66.     pthread_create(&Thread_02, NULL,(void*) Thread_02_main, NULL);
  67.    
  68.     /* Initialize the condition variable used   */
  69.     /* to communicate between Thread 01 & 02    */
  70.     Cond_Th01To2 = PTHREAD_COND_INITIALIZER;
  71.     Mutex_Th01To02 = PTHREAD_MUTEX_INITIALIZER;
  72.    
  73.     while(1)
  74.     {
  75.         ;
  76.     }
  77.        
  78.     return 0;
  79. }

Message cité 1 fois
Message édité par sined40 le 29-09-2013 à 23:11:14
mood
Publicité
Posté le 29-09-2013 à 22:32:16  profilanswer
 

n°2204666
breizhbugs
Posté le 30-09-2013 à 13:45:35  profilanswer
 

sined40 a écrit :

Bonjour,
Est-il possible de tuer mon main et de laisser les 2 threads en vie ?


 
Ben non, puisque les threads léger n'existent que tant que le processus auxquels ils appartiennent existe...
 
Pourquoi veux tu tuer le main? si c'est parce que ça fait un 'thread' en trop? utilise le main comme thread1 ?


Message édité par breizhbugs le 30-09-2013 à 13:46:15

---------------
Seul Google le sait...
n°2204742
sined40
Posté le 30-09-2013 à 19:01:31  profilanswer
 

Merci pour la réponse. Je vais utiliser mon main, pour monitorer les autres threads donc.


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  Threads

 

Sujets relatifs
threads et exceptions sur javaquestion sur les threads
Threads posix[Java] Threads et IPC : meilleure solution ?
Sockets et Threads sont sur un bateauutilisation des threads
threads en chat sur PythonThreads synchronisés ?
[C#] threads et communicationMFC & Threads problème de compréhension
Plus de sujets relatifs à : Threads


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR