c/c++ FIFO wait-free queue, easy built cross platform(no extra dependencies needed)
Guarantee thread safety memory management, and it's all in one header only, as fast as never wait.
#include "wfqueue.h"
tWaitFree::Queue<MyVal> myqueue(sz);
// wrap in to thread
new ClassVal s(...);
tWaitFree::WfqEnqCtx<MyVal> enqCtx; // init 1 time in a thread only
tWaitFree::WfqDeqCtx<MyVal> deqCtx; // init 1 time in a thread only
// wrap in to thread
// please use enq to guarantee enqueue.
if(myqueue.tryEnq(s, enqCtx)) {
printf("%s\n", "Enq Done");
} else {
printf("%s\n", "queue is full, please try again to re-enqueue ");
}
// wrap in to thread
// please use deq to guarantee dequeue.
if(myqueue.tryDeq(s, deqCtx) {
s->do_op();
}
#include "wfqueue.h"
// Fixed size of queue
wfqueue_t *q = wfq_create(sz);
wfq_enq_ctx_t enq_ctx = wfq_init_enq_ctx(); // init 1 time in a thread only
wfq_deq_ctx_t deq_ctx = wfq_init_deq_ctx(); // init 1 time in a thread only
// wrap in to thread
ClassVal *s = malloc(sizeof(ClassVal);
// wfq_enq_must for guarantee enqueue
wfq_enq_must(q, s, &enq_ctx);
// wrap in to thread
// wfq_deq_must for guarantee dequeue
s = (ClassVal*)wfq_deq(q, &deq_ctx); // return NULL if no val consuming
if(s) {
s->do_op();
free(s);
}
wfq_destroy(q);
include header file in your project
Type | WFQUEUE (c++) ms | WFQUEUE (c)ms | MOODYCAMEL (ms) | INPUTS PER THREAD |
---|---|---|---|---|
MPMC | 389.0 | 370 | 393.9 | 1,000,000 * 4 |
MPSC | 494.50 | 600 | 684.0 | 1,000,000 * 4 |
MCSP | 252.70 | 290.5 | 175.7 | 1,000,000 * 4 |
Type | WFQUEUE (c++) ms | WFQUEUE (c)ms | MOODYCAMEL (ms) | INPUTS PER THREAD |
---|---|---|---|---|
MPMC | 657.20 | 642.2 | 517.90 | 1,000,000 * 8 |
MPSC | 1140.50 | 1420 | 1471.8 | 1,000,000 * 8 |
MCSP | 247.80 | 245.2 | 253.2 | 1,000,000 * 8 |
Type | WFQUEUE (c++) ms | WFQUEUE (c)ms | MOODYCAMEL (ms) | INPUTS PER THREAD |
---|---|---|---|---|
MPMC | 499 | 496 | 517.90 | 1,000,000 * 8 |
MPSC | 1718 | 1700 | 1471.8 | 1,000,000 * 8 |
MCSP | 403 | 360 | 253.2 | 1,000,000 * 8 |
No pre allocated size needed, it will expand itself (expandable size)