mpmc-queue: run more producer/consumer threads
[model-checker-benchmarks.git] / mpmc-queue / mpmc-queue.cc
1 #include <inttypes.h>
2 #include <threads.h>
3 #include <stdio.h>
4
5 #include <librace.h>
6
7 #include "mpmc-queue.h"
8
9 void threadA(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
10 {
11         int32_t *bin = queue->write_prepare();
12         store_32(bin, 1);
13         queue->write_publish();
14 }
15
16 void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
17 {
18         int32_t *bin;
19         while (bin = queue->read_fetch()) {
20                 printf("Read: %d\n", load_32(bin));
21                 queue->read_consume();
22         }
23 }
24
25 int user_main(int argc, char **argv)
26 {
27         struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
28         thrd_t A1, A2, B1, B2;
29
30         int32_t *bin = queue.write_prepare();
31         store_32(bin, 17);
32         queue.write_publish();
33
34         printf("Start threads\n");
35
36         thrd_create(&A1, (thrd_start_t)&threadA, &queue);
37         thrd_create(&A2, (thrd_start_t)&threadA, &queue);
38         thrd_create(&B1, (thrd_start_t)&threadB, &queue);
39         thrd_create(&B2, (thrd_start_t)&threadB, &queue);
40         thrd_join(A1);
41         thrd_join(A2);
42         thrd_join(B1);
43         thrd_join(B2);
44
45         printf("Threads complete\n");
46
47         return 0;
48 }