Пример #1
0
// Exactly the same, but the inverse:
void *consumer(void *arg) {
  int print_item;
  while (1) {
    sleep( rand() % 5 );
    sem_wait(&sem_consumer);
    pthread_mutex_lock(&mut_buf);
      print_item = take_from_buf();
    sem_post(&sem_producer);
    pthread_mutex_unlock(&mut_buf);

    printf("%d \n", print_item);
    fflush(stdout);  // Force printing now; don't wait for the newline
  }
}
// Exactly the same, but the inverse:
void *consumer(void *arg) {
    while (1) {
        int work_item;
        sleep( rand() % 5 );
        sem_wait(&sem_consumer);
        //printf("\nAfter Cons Sem_Wait\n");
        pthread_mutex_lock(&mut_buf);
        work_item = take_from_buf();
        pthread_mutex_unlock(&mut_buf);
        printf("%d ", work_item);
        fflush(stdout);  // Force printing now; don't wait for the newline
        sem_post(&sem_producer);

    }
}