action: improve "unintialized load" bug print
[model-checker.git] / test / fences.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.h>
4
5 #include "librace.h"
6
7 atomic_int x;
8 atomic_int y;
9
10 static void a(void *obj)
11 {
12         atomic_store_explicit(&x, 1, memory_order_relaxed);
13         atomic_store_explicit(&x, 2, memory_order_relaxed);
14         atomic_thread_fence(memory_order_seq_cst);
15         printf("Thread A reads: %d\n", atomic_load_explicit(&y, memory_order_relaxed));
16 }
17
18 static void b(void *obj)
19 {
20         atomic_store_explicit(&y, 1, memory_order_relaxed);
21         atomic_store_explicit(&y, 2, memory_order_relaxed);
22         atomic_thread_fence(memory_order_seq_cst);
23         printf("Thread B reads: %d\n", atomic_load_explicit(&x, memory_order_relaxed));
24 }
25
26 int user_main(int argc, char **argv)
27 {
28         thrd_t t1, t2;
29
30         atomic_init(&x, 0);
31         atomic_init(&y, 0);
32
33         printf("Main thread: creating 2 threads\n");
34         thrd_create(&t1, (thrd_start_t)&a, NULL);
35         thrd_create(&t2, (thrd_start_t)&b, NULL);
36
37         thrd_join(t1);
38         thrd_join(t2);
39         printf("Main thread is finishing\n");
40
41         return 0;
42 }