-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions-raceConditions.c
62 lines (53 loc) · 1.52 KB
/
transactions-raceConditions.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
/*
Handles simple transactions and avoids race conditions
with the help of mutexes when interacting with critical regions
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
double bankAccountBalance = 0;
bool lock = 1;
void deposit(double amount) {
pthread_mutex_lock(&lock);
bankAccountBalance += amount;
pthread_mutex_unlock(&lock);
}
void withdraw(double amount) {
pthread_mutex_lock(&lock);
bankAccountBalance -= amount;
pthread_mutex_unlock(&lock);
}
unsigned odd(unsigned long num) {
return num % 2;
}
void do1000Transactions(unsigned long id) {
for (int i = 0; i < 1000; i++) {
if (odd(id))
deposit(100.00);
else
withdraw(100.00);
}
}
void* child(void* buf) {
unsigned long childID = (unsigned long)buf;
do1000Transactions(childID);
return NULL;
}
int main(int argc, char** argv) {
pthread_t *children;
unsigned long id = 0;
unsigned long nThreads = 0;
if (argc > 1)
nThreads = atoi(argv[1]);
children = malloc( nThreads * sizeof(pthread_t) );
for (id = 1; id < nThreads; id++)
pthread_create(&(children[id-1]), NULL, child, (void*)id);
do1000Transactions(0); // main thread work (id=0)
for (id = 1; id < nThreads; id++)
pthread_join(children[id-1], NULL);
printf("\nThe final account balance with %lu threads is $%.2f.\n\n", nThreads, bankAccountBalance);
free(children);
pthread_mutex_destroy(&lock);
return 0;
}