8 /* Can be tested for different behavior with relaxed vs. release/acquire/seq-cst */
9 #define load_mo std::memory_order_relaxed
10 #define store_mo std::memory_order_relaxed
12 static std::atomic_int *x;
14 static void a(void *obj)
16 int idx = *((int *)obj);
19 x[idx - 1].load(load_mo);
22 x[idx].store(1, store_mo);
27 int user_main(int argc, char **argv)
35 printf("Error: must have N >= 2\n");
40 threads = (thrd_t *)malloc((N + 1) * sizeof(thrd_t));
41 x = (std::atomic_int *)malloc(N * sizeof(std::atomic_int));
42 indexes = (int *)malloc((N + 1) * sizeof(int));
44 for (int i = 0; i < N + 1; i++)
47 for (int i = 0; i < N; i++)
48 atomic_init(&x[i], 0);
50 for (int i = 0; i < N + 1; i++)
51 thrd_create(&threads[i], (thrd_start_t)&a, (void *)&indexes[i]);
53 for (int i = 0; i < N + 1; i++)
54 thrd_join(threads[i]);