-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mutex.c
57 lines (46 loc) · 1.36 KB
/
test_mutex.c
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 5
/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
int tid;
double stuff;
} thread_data_t;
/* shared data between threads */
double shared_x;
pthread_mutex_t lock_x;
void *thr_func(void *arg) {
thread_data_t *data = (thread_data_t *)arg;
printf("hello from thr_func, thread id: %d\n", data->tid);
/* get mutex before modifying and printing shared_x */
pthread_mutex_lock(&lock_x);
shared_x += data->stuff;
printf("x = %f\n", shared_x);
pthread_mutex_unlock(&lock_x);
pthread_exit(NULL);
}
int main(int argc, char **argv) {
pthread_t thr[NUM_THREADS];
int i, rc;
/* create a thread_data_t argument array */
thread_data_t thr_data[NUM_THREADS];
/* initialize shared data */
shared_x = 0;
/* initialize pthread mutex protecting "shared_x" */
pthread_mutex_init(&lock_x, NULL);
/* create threads */
for (i = 0; i < NUM_THREADS; ++i) {
thr_data[i].tid = i;
thr_data[i].stuff = (i + 1) * NUM_THREADS;
if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) {
fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
return EXIT_FAILURE;
}
}
/* block until all threads complete */
for (i = 0; i < NUM_THREADS; ++i) {
pthread_join(thr[i], NULL);
}
return EXIT_SUCCESS;
}