-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultithreads_deadlock.c
90 lines (72 loc) · 1.92 KB
/
multithreads_deadlock.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <pthread.h>
/* number of threads */
#define N 2
/* global variables incremented by threads */
uint32_t g1 = 0, g2 = 0;
/* global mutexes protecting g1 and g2, respectively */
pthread_mutex_t mutex1, mutex2;
/* function to be executed by all threads */
void *my_thread(void *vargp)
{
uint32_t my_id = (uintptr_t)vargp;
uint32_t iterations = 10000;
while (iterations--)
{
uint32_t rounds = 10000, g;
if (my_id == 1)
{
/* thread 1 only */
pthread_mutex_lock(&mutex1);
g = g1;
/* sleep 0.1 milliseconds */
usleep(100);
pthread_mutex_lock(&mutex2);
g2 = ++g;
pthread_mutex_unlock(&mutex2);
pthread_mutex_unlock(&mutex1);
}
else
{
/* thread 2 only */
pthread_mutex_lock(&mutex2);
g = g2;
/* sleep 0.1 milliseconds */
usleep(100);
pthread_mutex_lock(&mutex1);
g1 = ++g;
pthread_mutex_unlock(&mutex1);
pthread_mutex_unlock(&mutex2);
}
/* sleep 0.9 milliseconds */
usleep(900);
printf("Thread %u: g = %5u\n", my_id, g);
}
}
int main()
{
pthread_t tid[N];
uint32_t i, id;
/* init mutexes */
pthread_mutex_init(&mutex1, NULL);
pthread_mutex_init(&mutex2, NULL);
/* create N threads */
for (i = 0; i < N; i++)
{
id = i + 1;
pthread_create(&tid[i], NULL, my_thread, (void*)(uintptr_t)id);
printf("Started thread %d: %p\n", id, (void *)tid[i]);
}
/* wait for threads to terminate */
for (i = 0; i < N; i++)
{
pthread_join(tid[i], NULL);
}
/* deinit mutex */
pthread_mutex_destroy(&mutex1);
pthread_mutex_destroy(&mutex2);
exit(0);
}