mpmc-queue: improve driver, to read all items from queue
[model-checker-benchmarks.git] / mpmc-queue / mpmc-queue.cc
index b6af7ef7020f8f5f02812701d8eea07e99663c0f..17713cbfb5f3f162a91c2117e7907b11abe21681 100644 (file)
@@ -1,3 +1,4 @@
+#include <inttypes.h>
 #include <threads.h>
 #include <stdio.h>
 
@@ -5,29 +6,39 @@
 
 #include "mpmc-queue.h"
 
-void threadA(struct mpmc_boundq_1_alt<int, sizeof(int)> *queue)
+void threadA(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
 {
-       int *bin = queue->write_prepare();
-       *bin = 1;
+       int32_t *bin = queue->write_prepare();
+       store_32(bin, 1);
        queue->write_publish();
 }
 
-void threadB(struct mpmc_boundq_1_alt<int, sizeof(int)> *queue)
+void threadB(struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> *queue)
 {
-       int *bin = queue->read_fetch();
-       printf("Read: %d\n", *bin);
-       queue->read_consume();
+       int32_t *bin;
+       while (bin = queue->read_fetch()) {
+               printf("Read: %d\n", load_32(bin));
+               queue->read_consume();
+       }
 }
 
 int user_main(int argc, char **argv)
 {
-       struct mpmc_boundq_1_alt<int, sizeof(int)> queue;
+       struct mpmc_boundq_1_alt<int32_t, sizeof(int32_t)> queue;
        thrd_t A, B;
 
+       int32_t *bin = queue.write_prepare();
+       store_32(bin, 17);
+       queue.write_publish();
+
+       printf("Start threads\n");
+
        thrd_create(&A, (thrd_start_t)&threadA, &queue);
        thrd_create(&B, (thrd_start_t)&threadB, &queue);
        thrd_join(A);
        thrd_join(B);
 
+       printf("Threads complete\n");
+
        return 0;
 }