-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncDeposit.c
154 lines (115 loc) · 3.75 KB
/
SyncDeposit.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
// this structure is for balance, condition variable and mutex.
typedef struct {
int balance;
pthread_mutex_t *lock;
pthread_cond_t *isEnough;
} Balance;
// pointer to Balance instance is located in datasegment.
Balance *myBalance;
int retVal;
// initializing Balance structure
void balanceInit(void) {
// all the data of Balance instance is located in heap.
myBalance = (Balance *)malloc(sizeof(Balance));
myBalance->balance = 0;
myBalance->lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
retVal = pthread_mutex_init(myBalance->lock, NULL);
if (retVal) {
fprintf(stderr, "pthread_mutex_init error\n");
exit(-1);
}
myBalance->isEnough = (pthread_cond_t *)malloc(sizeof(pthread_cond_t));
retVal = pthread_cond_init(myBalance->isEnough, NULL);
if (retVal) {
fprintf(stderr, "pthread_cond_init error\n");
exit(-1);
}
}
// returning all the resources as mutex, conditional var, myBalance…
// destroy should be executed before calling free().
void balanceDelete(void) {
retVal = pthread_mutex_destroy(myBalance->lock);
if (retVal) {
fprintf(stderr, "pthread_mutex_destroy error\n");
exit(-1);
}
free(myBalance->lock);
retVal = pthread_cond_destroy(myBalance->isEnough);
if (retVal) {
fprintf(stderr, "pthread_cond_destroy error\n");
exit(-1);
}
free(myBalance->isEnough);
free(myBalance);
}
// thread for deposit
void *depositThread(void *arg) {
while (1) {
// making random number between 1 ~ 10.
int amount = rand() % 10 + 1;
// acquire lock for accessing shared resource.
pthread_mutex_lock(myBalance->lock);
myBalance->balance += amount;
printf("deposit %d\t\t\t\t\t%d\n", amount, myBalance->balance);
// pthread_cond_signal is ok concerning the relative location with
// pthread_mutex_unlock
pthread_cond_signal(myBalance->isEnough);
pthread_mutex_unlock(myBalance->lock);
// after deposit, sleep is called to allow withdraw.
sleep(1);
}
return NULL;
}
// thread for withdraw.
void *withdrawThread(void *arg) {
while (1) {
int amount = rand() % 10 + 1;
pthread_mutex_lock(myBalance->lock);
// if balance is less than amount to withdraw, it should wait signal
// which is sent after deposit
// while is used to recheck whether the balance is enough.
while (myBalance->balance < amount) {
printf("\t\twait for deposit\n");
pthread_cond_wait(myBalance->isEnough, myBalance->lock);
}
// balance is enough so can withdraw.
myBalance->balance -= amount;
printf("\t\t\twithdraw %d\t\t%d\n", amount, myBalance->balance);
// all done, unlock is executed.
pthread_mutex_unlock(myBalance->lock);
}
return NULL;
}
int main() {
pthread_t deposit, withdraw;
balanceInit();
printf("deposit\t\twithdraw\t\tbalance\n");
// create threads
retVal = pthread_create(&deposit, NULL, depositThread, NULL);
if (retVal) {
fprintf(stderr, "pthread_create error\n");
exit(-1);
}
retVal = pthread_create(&withdraw, NULL, withdrawThread, NULL);
if (retVal) {
fprintf(stderr, "pthread_create error\n");
exit(-1);
}
// wait for threads to exit.
retVal = pthread_join(deposit, NULL);
if (retVal) {
fprintf(stderr, "pthread_join error\n");
exit(-1);
}
retVal = pthread_join(withdraw, NULL);
if (retVal) {
fprintf(stderr, "pthread_join error\n");
exit(-1);
}
balanceDelete();
return 0;
}