Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. static int sum_stat_a = 0;
  2. static int sum_stat_b = 0;
  3. int aggregateStats(int stat_a, int stat_b) {
  4.     pthread_mutex_lock(&mut);
  5.     //wait for other thread to finish
  6.         while (sum_stat_a == 0 && sum_stat_b == 0){
  7.             pthread_cond_wait(&cond, &condlock);
  8.         }
  9.         pthread_mutex_unlock(&condlock);
  10.         pthread_mutex_lock(&mut);
  11.        
  12.     sum_stat_a += stat_a;
  13.     sum_stat_b += stat_b;
  14.     return sum_stat_a + sum_stat_b;
  15.  
  16.         pthread_mutex_unlock(&mut);
  17.    
  18. }
  19. void init(void)
  20. {
  21.     pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  22.     pthread_mutex_t condlock;
  23.     pthread_mutex_t mut;
  24.     pthread_mutex_init(&condlock, NULL);
  25.     pthread_mutex_init(&mut, NULL);
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement