model-assert: include <stdbool.h>
[c11tester.git] / test / litmus / seq-lock.cc
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4 #include <atomic>
5
6 std::atomic_int x;
7 std::atomic_int y;
8 std::atomic_int z;
9
10 static int N = 1;
11
12 static void a(void *obj)
13 {
14         for (int i = 0; i < N; i++) {
15                 x.store(2 * i + 1, std::memory_order_release);
16                 y.store(i + 1, std::memory_order_release);
17                 z.store(i + 1, std::memory_order_release);
18                 x.store(2 * i + 2, std::memory_order_release);
19         }
20 }
21
22 static void b(void *obj)
23 {
24         printf("x: %d\n", x.load(std::memory_order_acquire));
25         printf("y: %d\n", y.load(std::memory_order_acquire));
26         printf("z: %d\n", z.load(std::memory_order_acquire));
27         printf("x: %d\n", x.load(std::memory_order_acquire));
28 }
29
30 int user_main(int argc, char **argv)
31 {
32         thrd_t t1, t2;
33
34         if (argc > 1)
35                 N = atoi(argv[1]);
36
37         printf("N: %d\n", N);
38
39         atomic_init(&x, 0);
40         atomic_init(&y, 0);
41         atomic_init(&z, 0);
42
43         printf("Main thread: creating 2 threads\n");
44         thrd_create(&t1, (thrd_start_t)&a, NULL);
45         thrd_create(&t2, (thrd_start_t)&b, NULL);
46
47         thrd_join(t1);
48         thrd_join(t2);
49         printf("Main thread is finished\n");
50
51         return 0;
52 }