prune mod order
[c11tester.git] / pthread_test / uninit.cc
1 /**
2  * @file uninit.cc
3  * @brief Uninitialized loads test
4  *
5  * This is a test of the "uninitialized loads" code. While we don't explicitly
6  * initialize y, this example's synchronization pattern should guarantee we
7  * never see it uninitialized.
8  */
9 #include <stdio.h>
10 #include <pthread.h>
11 #include <atomic>
12
13 //#include "librace.h"
14
15 std::atomic_int x;
16 std::atomic_int y;
17
18 static void *a(void *obj)
19 {
20         int flag = x.load(std::memory_order_acquire);
21         printf("flag: %d\n", flag);
22         if (flag == 2)
23                 printf("Load: %d\n", y.load(std::memory_order_relaxed));
24         return NULL;
25 }
26
27 static void *b(void *obj)
28 {
29         printf("fetch_add: %d\n", x.fetch_add(1, std::memory_order_relaxed));
30         return NULL;
31 }
32
33 static void *c(void *obj)
34 {
35         y.store(3, std::memory_order_relaxed);
36         x.store(1, std::memory_order_release);
37         return NULL;
38 }
39
40 int user_main(int argc, char **argv)
41 {
42         pthread_t t1, t2, t3;
43
44         std::atomic_init(&x, 0);
45
46         printf("Main thread: creating 3 threads\n");
47         pthread_create(&t1,NULL, &a, NULL);
48         pthread_create(&t2,NULL, &b, NULL);
49         pthread_create(&t3,NULL, &c, NULL);
50
51         pthread_join(t1,NULL);
52         pthread_join(t2,NULL);
53         pthread_join(t3,NULL);
54         printf("Main thread is finished\n");
55
56         return 0;
57 }