Advertisement
KDOXG

SO Prova Gerson Questão 2

Dec 16th, 2019
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #define MAX 1000000000
  2. Elemento buffer[MAX];
  3. int nelem = 0;
  4. pthread_mutex_t m;
  5. pthread_cond_t c;
  6.  
  7. void* Produtor(void* dta)
  8. {
  9.     Elemento *e;
  10.     while(1)
  11.     {
  12.         e = produzElemento(buffer);
  13.         pthread_mutex_lock(&m);
  14.         insereElemento(e);
  15.         nelem++;
  16.         pthread_cond_signal(&c);
  17.     pthread_mutex_unlock(&m);
  18.     }
  19.  
  20.     return NULL;
  21. }
  22.  
  23. void* Consumidor(void* dta)
  24. {
  25.     Elemento *e;
  26.     while(1)
  27.     {
  28.         pthread_mutex_lock(&m);
  29.         while (nelem == 0)
  30.         {
  31.             pthread_mutex_unlock(&m);
  32.             pthread_cond_wait(&c);
  33.             pthread_mutex_lock(&m);
  34.         }
  35.         e = retiraElemento(buffer);
  36.         nelem--;
  37.         pthread_mutex_unlock(&m);
  38.         consomeElemento(e);
  39.     }
  40.  
  41.     return NULL;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement