README.md: add linux locks example
[model-checker.git] / test / mutextest.cc
1 #include <stdio.h>
2
3 #include "threads.h"
4 #include "librace.h"
5 #include "stdatomic.h"
6 #include <mutex>
7 std::mutex * m;
8 int shareddata;
9
10 static void a(void *obj)
11 {
12         int i;
13         for(i=0;i<2;i++) {
14                 if ((i%2)==0) {
15                         m->lock();
16                         store_32(&shareddata,(unsigned int)i);
17                         m->unlock();
18                 } else {
19                         while(!m->try_lock())
20                                 thrd_yield();
21                         store_32(&shareddata,(unsigned int)i);
22                         m->unlock();
23                 }
24         }
25 }
26
27 int user_main(int argc, char **argv)
28 {
29         thrd_t t1, t2;
30         m=new std::mutex();
31
32         thrd_create(&t1, (thrd_start_t)&a, NULL);
33         thrd_create(&t2, (thrd_start_t)&a, NULL);
34
35         thrd_join(t1);
36         thrd_join(t2);
37         return 0;
38 }