16d2b59424398ef62614a5201e6767d3ad7e7418
[c11tester.git] / pthread_test / bug1.cc
1 /**
2  * @file iriw.cc
3  * @brief Independent read and independent write test
4  */
5
6 #include <atomic>
7 #include <pthread.h>
8 #include <stdio.h>
9
10 #define N 14
11 //#include "wildcard.h"
12 //#include "model-assert.h"
13
14 using namespace std;
15
16 std::atomic_int x, y;
17 int r1, r2, r3, r4; /* "local" variables */
18
19 static void *a(void *obj)
20 {
21 //      x.store(1, memory_order_seq_cst);
22         return NULL;
23 }
24
25
26 static void *b(void *obj)
27 {
28         y.store(1, memory_order_seq_cst);
29         return NULL;
30 }
31
32 static void *c(void *obj)
33 {
34         r1 = x.load(memory_order_acquire);
35         r2 = y.load(memory_order_seq_cst);
36         return NULL;
37 }
38
39 static void *d(void *obj)
40 {
41         r3 = y.load(memory_order_acquire);
42         r4 = x.load(memory_order_seq_cst);
43         return NULL;
44 }
45
46
47 int user_main(int argc, char **argv)
48 {
49         pthread_t threads[20];
50
51         atomic_init(&x, 0);
52         atomic_init(&y, 0);
53
54         printf("Main thread: creating %d threads\n", N);
55
56         for (int i = 0; i< N; i++)
57                 pthread_create(&threads[i],NULL, &a, NULL);
58
59         for (int i=0; i<N; i++)
60                 printf("thread id: %ld\n", threads[i]);
61
62         for (int i = 0; i< N; i++)
63                 pthread_join( threads[i],NULL);
64
65         printf("Main thread is finished\n");
66
67         /*
68          * This condition should not be hit if the execution is SC */
69 //      bool sc = (r1 == 1 && r2 == 0 && r3 == 1 && r4 == 0);
70 //      printf("r1 = %d, r2 = %d, r3 = %d and r4 = %d\n", r1, r2, r3, r4);
71 //      MODEL_ASSERT(!sc);
72
73         return 0;
74 }