Merge branch 'master' of /home/git/model-checker
[model-checker.git] / test / deadlock.cc
1 #include <stdio.h>
2 #include <threads.h>
3 #include <mutex>
4
5 #include "librace.h"
6
7 std::mutex *x;
8 std::mutex *y;
9 uint32_t shared = 0;
10
11 static void a(void *obj)
12 {
13         x->lock();
14         y->lock();
15         printf("shared = %u\n", load_32(&shared));
16         y->unlock();
17         x->unlock();
18 }
19
20 static void b(void *obj)
21 {
22         y->lock();
23         x->lock();
24         store_32(&shared, 16);
25         printf("write shared = 16\n");
26         x->unlock();
27         y->unlock();
28 }
29
30 int user_main(int argc, char **argv)
31 {
32         thrd_t t1, t2;
33
34         x = new std::mutex();
35         y = new std::mutex();
36
37         printf("Thread %d: creating 2 threads\n", thrd_current());
38         thrd_create(&t1, (thrd_start_t)&a, NULL);
39         thrd_create(&t2, (thrd_start_t)&b, NULL);
40
41         thrd_join(t1);
42         thrd_join(t2);
43         printf("Thread %d is finished\n", thrd_current());
44
45         return 0;
46 }