-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.c
More file actions
42 lines (35 loc) · 998 Bytes
/
counter.c
File metadata and controls
42 lines (35 loc) · 998 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
/*
* synchronized counter
* based on Operating Systems: Three Easy Pieces by R. Arpaci-Dusseau and A. Arpaci-Dusseau
*
* This example uses locks and a condition to synchronize a child thread and
* the parent. The child generates matrices and the parent computes the
* average when the matrix is ready for processing.
*
* Wes J. Lloyd
* University of Washington, Tacoma
* TCSS 422 - Operating Systems
* Spring 2020
*/
// Include libraries required for this module only
#include <stdio.h>
#include <pthread.h>
#include "counter.h"
// SYNCHRONIZED COUNTER METHOD IMPLEMENTATION
// Based on Three Easy Pieces
void init_counter(counter_t *c) {
c->value = 0;
pthread_mutex_init(&c->lock, NULL);
}
int inc_counter(counter_t *c) {
pthread_mutex_lock(&c->lock);
int rc = ++c->value;
pthread_mutex_unlock(&c->lock);
return rc;
}
int get_counter(counter_t *c) {
pthread_mutex_lock(&c->lock);
int rc = c->value;
pthread_mutex_unlock(&c->lock);
return rc;
}