-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
43 lines (39 loc) · 961 Bytes
/
example.c
File metadata and controls
43 lines (39 loc) · 961 Bytes
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
39
40
41
42
43
#include <stdio.h>
#include <stdlib.h>
#include "lock.h"
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int resource = 0;
void *producer(void *arg)
{
for (int i = 0; i < 5; i++)
{
LOCK_SIGNAL(&mutex, &cond, {
WAIT(&mutex, &cond, resource != 0);
resource = i + 1;
printf("Producer: produced %d\n", resource);
});
}
return NULL;
}
void *consumer(void *arg)
{
for (int i = 0; i < 5; i++)
{
LOCK_SIGNAL(&mutex, &cond, {
WAIT(&mutex, &cond, resource == 0);
printf("Consumer: consumed %d\n", resource);
resource = 0;
});
}
return NULL;
}
int main()
{
pthread_t prod_tid, cons_tid;
pthread_create(&prod_tid, NULL, producer, NULL);
pthread_create(&cons_tid, NULL, consumer, NULL);
pthread_join(prod_tid, NULL);
pthread_join(cons_tid, NULL);
return 0;
}