// Copyright 2021 Jeisson Hidalgo-Cespedes CC-BY-4 #include #include #include "common.h" #include "consumer.h" void* consume(void* data) { simulation_t* simulation = (simulation_t*)data; while (true) { // lock(can_access_consumed_count) pthread_mutex_lock(&simulation->can_access_consumed_count); if (simulation->consumed_count < simulation->unit_count) { // Reserve the next product to me ++simulation->consumed_count; } else { // unlock(can_access_consumed_count) pthread_mutex_unlock(&simulation->can_access_consumed_count); // break while break; } // unlock(can_access_consumed_count) pthread_mutex_unlock(&simulation->can_access_consumed_count); // wait(can_consume) sem_wait(&simulation->can_consume); size_t value = 0; queue_dequeue(&simulation->queue, &value); printf("\tConsuming %zu\n", value); usleep(1000 * random_between(simulation->consumer_min_delay , simulation->consumer_max_delay)); } return NULL; }