-
Notifications
You must be signed in to change notification settings - Fork 0
/
reentrant.c
82 lines (63 loc) · 1.77 KB
/
reentrant.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <err.h>
#define INIT_MONEY 100000
#define ITERATIONS 100
typedef struct {
int id;
double balance;
pthread_mutex_t lock;
} account;
typedef struct {
account *a1;
account *a2;
int iterations;
} worker;
void initialize_account(account *a, int id, double balance) {
a->id = id;
a->balance = balance;
pthread_mutexattr_t attr;
if(pthread_mutexattr_init(&attr))
errx(-1, "pthread_mutexattr_init");
if(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
errx(-1, "pthread_mutexattr_settype");
if(pthread_mutex_init(&a->lock, &attr))
errx(-1, "pthread_mutex_init");
}
void transfer(account *from, account *to, double amount) {
pthread_mutex_t *lock1 = &from->lock;
pthread_mutex_t *lock2 = &to->lock;
if(from->id < to->id) {
lock1 = &to->lock;
lock2 = &from->lock;
}
/* Ok to take twice the same lock, it's reentrant */
pthread_mutex_lock(lock1);
pthread_mutex_lock(lock2);
if(from->balance >= amount) {
from->balance -= amount;
to->balance += amount;
}
pthread_mutex_unlock(lock2);
pthread_mutex_unlock(lock1);
}
void *thread_fn(void *data) {
worker *w = (worker *)data;
for(int i=0; i<w->iterations; i++) {
transfer(w->a1, w->a2, 10.0);
transfer(w->a2, w->a1, 10.0);
}
pthread_exit(NULL);
}
int main(int argc, char **argv) {
account account1;
pthread_t t1;
initialize_account(&account1, 1, INIT_MONEY);
worker w1 = {&account1, &account1, ITERATIONS};
if(pthread_create(&t1, NULL, thread_fn, (void *)&w1))
errx(-1, "pthread_create");
if(pthread_join(t1, NULL))
errx(-1, "pthread_join");
return 0;
}