README.md: add linux locks example
[model-checker.git] / test / csetest.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.h>
4
5 #include "librace.h"
6
7 atomic_int a;
8 atomic_int b;
9
10 static void r(void *obj)
11 {
12         int r1=atomic_load_explicit(&a, memory_order_relaxed);
13         int r2=atomic_load_explicit(&a, memory_order_relaxed);
14         if (r1==r2)
15                 atomic_store_explicit(&b, 2, memory_order_relaxed);
16         printf("r1=%d\n",r1);
17         printf("r2=%d\n",r2);
18 }
19
20 static void s(void *obj)
21 {
22         int r3=atomic_load_explicit(&b, memory_order_relaxed);
23         atomic_store_explicit(&a, r3, memory_order_relaxed);
24         printf("r3=%d\n",r3);
25 }
26
27 int user_main(int argc, char **argv)
28 {
29         thrd_t t1, t2;
30
31         atomic_init(&a, 0);
32         atomic_init(&b, 1);
33
34         printf("Main thread: creating 2 threads\n");
35         thrd_create(&t1, (thrd_start_t)&r, NULL);
36         thrd_create(&t2, (thrd_start_t)&s, NULL);
37
38         thrd_join(t1);
39         thrd_join(t2);
40         printf("Main thread is finished\n");
41
42         return 0;
43 }