while (reports_in < 2) { printf("MAIN: waiting for flag\n"); pthread_cond_wait(&flag, &lock); printf("MAIN: I have the lock\n"); printf("%d\n", mailbox->count); total += mailbox->count; if (mailbox == &a1) pthread_join(t1, NULL); if (mailbox == &a2) pthread_join(t2, NULL); mailbox = NULL; pthread_cond_signal(&flag); reports_in ++; }
printf("total %d\n", total);
return0; }
void * add2(void * a) { structarg_set *arg = a; for (int i = 0; i < arg->times; i++) { arg->count++; }
printf("COUNT: waiting to get lock\n"); pthread_mutex_lock(&lock); printf("COUNT: have lock\n"); if (mailbox != NULL) pthread_cond_wait(&flag, &lock); mailbox = arg; pthread_cond_signal(&flag); pthread_mutex_unlock(&lock);