Advertisement
Guest User

signal_wait.c

a guest
Jan 14th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include<string.h>
  5. #include<pthread.h>
  6. #include<time.h>
  7.  
  8. #define N 2
  9.  
  10. /*
  11.     Come usare signal e wait
  12.    
  13.     pthread_cond_broadcast(&cond); --> //sveglia tutti i threads che hanno chiamato wait su quella condizione
  14.     pthread_cond_signal(&cond); --> //sveglia uno qualsiasi dei thread che dorme sulla condizione
  15.     pthread_cond_wait(&cond, &mut); //mi addormento su quella condizione, fa unlock, al momento del risveglio è già in lock
  16.    
  17. */
  18.  
  19. int x, y;
  20. int addormentato = 0;
  21. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  22. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23.  
  24. int retcode = 999;
  25.  
  26. void* incrementax(void* arg)
  27. {  
  28.     for(;;) {      
  29.         pthread_mutex_lock(&mutex);
  30.        
  31.         if (x > 200) {
  32.             printf("x ha finito\n");
  33.             pthread_mutex_unlock(&mutex);
  34.             pthread_exit((void *) &retcode);
  35.         }
  36.        
  37.         if (x > (y+10)) {
  38.             printf("x %d > y %d\n", x, y);
  39.             printf("thread tx: zZzZzZzZ\n");
  40.             addormentato = 1;
  41.             pthread_cond_wait(&cond,&mutex);
  42.             addormentato = 0;
  43.             printf("tx: svegliato con x=%d\n", x);
  44.             pthread_mutex_unlock(&mutex);
  45.         } else {
  46.             x = x + 3;
  47.             printf("thread tx: x = %d\n", x);
  48.             pthread_mutex_unlock(&mutex);
  49.         }
  50.         usleep(20000L);
  51.     }
  52. }
  53.        
  54. void* incrementay(void* arg)
  55. {
  56.     for(;;) {      
  57.         pthread_mutex_lock(&mutex);
  58.  
  59.         if (y > 200) {
  60.             printf("\t\tty ha finito\n");
  61.             pthread_mutex_unlock(&mutex);
  62.             pthread_exit(NULL);
  63.         }
  64.    
  65.         if (y > x) {
  66.             if (addormentato == 1) {
  67.                 printf("x %d < y %d\n", x, y);
  68.                 printf("\t\tthread ty: BASTA RONFARE\n");
  69.                 pthread_cond_signal(&cond);
  70.             }
  71.         }
  72.         y++;
  73.         printf("\t\tthread ty: y = %d\n", y);                      
  74.         pthread_mutex_unlock(&mutex);
  75.         usleep(50000L);
  76.     }  
  77. }
  78.  
  79.  
  80. int main(int argc, char** argv)
  81. {
  82.     int * presult;   // per valore restituito da pthread_exit(tx,...)
  83.     x = 0;
  84.     y = 5;
  85.     pthread_t tx, ty;
  86.    
  87.     pthread_create(&tx, NULL, incrementax, NULL ); 
  88.     pthread_create(&ty, NULL, incrementay, NULL);
  89.  
  90.     pthread_join(tx, (void **) &presult);
  91.     pthread_join(ty, NULL);
  92.    
  93.     printf("thread terminati, tx ritorna %d\n", *presult);
  94.    
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement