spsc example
[model-checker-benchmarks.git] / spsc-example / spsc-queue.cc
1 #include <threads.h>
2
3 #include "queue.h"
4
5 spsc_queue *q;
6 atomic_int arr[2];
7
8 void thread(unsigned thread_index)
9 {
10         if (0 == thread_index)
11         {
12                 arr[1].store(memory_order_relaxed);
13                 q->enqueue(1);
14         }
15         else
16         {
17                 int val = 0;
18                 bool succ = q->dequeue(&val);
19                 arr[val].load(memory_order_relaxed);
20         }
21 }
22
23 int user_main(int argc, char **argv)
24 {
25         thrd_t A, B;
26
27         q = new spsc_queue();
28
29         thrd_create(&A, (thrd_start_t)&thread, (void *)0);
30         thrd_create(&B, (thrd_start_t)&thread, (void *)1);
31         thrd_join(A);
32         thrd_join(B);
33
34         delete q;
35
36         return 0;
37 }