sined40 | 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 :
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- pthread_cond_t Cond_Th01To2;
- pthread_mutex_t Mutex_Th01To02;
- void* Thread_01_main(void);
- void* Thread_02_main(void);
- void* Thread_01_main(void)
- {
- printf("Execution Thread_01 routine\n" );
-
- while(1)
- {
- printf("Thread_01: Locking the mutex.\n" );
-
- /* Locking the mutex */
- pthread_mutex_lock(&Mutex_Th01To02);
-
- /* Waiting 5 seconds before sending the signal */
- sleep(5);
-
- printf("Thread_01: Sending the signal.\n" );
-
- /* Send a signal to wake up the 2nd Thread */
- pthread_cond_signal(&Cond_Th01To2);
-
- printf("Thread_01: Unlocking the mutex.\n" );
-
- /* Unlock the mutex */
- pthread_mutex_unlock(&Mutex_Th01To02);
- }
- return NULL;
- }
- void* Thread_02_main(void)
- {
- printf("Execution of Thread_02 routine\n" );
-
- while(1)
- {
- printf("Thread_02: Locking the mutex.\n" );
-
- /* Locking the mutex */
- pthread_mutex_lock(&Mutex_Th01To02);
-
- printf("Thread_02: Waiting the signal.\n" );
- /* Waiting the signal to wake up*/
- pthread_cond_wait(&Cond_Th01To2, &Mutex_Th01To02);
-
- /* Receiving the signal */
- printf("Thread_02: Receiving signal.\n" );
-
- /* Unlocking the mutex */
- pthread_mutex_unlock(&Mutex_Th01To02);
-
- /* Receiving the signal */
- printf("Thread_02: Unlocking the mutex.\n" );
- }
-
- return NULL;
- }
- int main(void)
- {
- pthread_t Thread_01;
- pthread_t Thread_02;
- pthread_create(&Thread_01, NULL,(void*) Thread_01_main, NULL);
- pthread_create(&Thread_02, NULL,(void*) Thread_02_main, NULL);
-
- /* Initialize the condition variable used */
- /* to communicate between Thread 01 & 02 */
- Cond_Th01To2 = PTHREAD_COND_INITIALIZER;
- Mutex_Th01To02 = PTHREAD_MUTEX_INITIALIZER;
-
- while(1)
- {
- ;
- }
-
- return 0;
- }
|
|