1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 | // Copyright 2021 Jeisson Hidalgo-Cespedes <jeisson.hidalgo@ucr.ac.cr> CC-BY-4
#include <stdbool.h>
#include <stdio.h>
#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;
}
|