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.
// Fixed size of queue
wfqueue_t *wfq_create(size_t fixed_size);
int wfq_enq(wfqueue_t *q, void* val);
void* wfq_deq(wfqueue_t *q);
void wfq_destroy(wfqueue_t *q);
size_t wfq_size(wfqueue_t *q);
size_t wfq_capacity(wfqueue_t *q);
#include "wfqueue.h"
// Fixed size of queue
wfqueue_t *q = wfq_create(fixed_sz);
// wrap in to thread
new ClassVal *s = new ClassVal;
wfq_enq(q, s); // or malloc if c programming, return 1 if success enqueue
// wrap in to thread
s = (ClassVal*)wfq_deq(q); // return NULL/nullptr if no val consuming
if(s) {
s->do_op();
delete s;
}
wfq_destroy(q);
#include "wfqueue.h"
// Fixed size of queue
wfqueue_t *q = wfq_create(fixed_sz);
// wrap in to thread
ClassVal *s = malloc(sizeof(ClassVal);
wfq_enq(q, s); // or malloc if c programming, return 1 if success enqueue
// wrap in to thread
s = (ClassVal*)wfq_deq(q); // return NULL/nullptr if no val consuming
if(s) {
s->do_op();
free(s);
}
wfq_destroy(q);
include header file in your project
class MyQueue {
wfqueue_t *q;
public:
MyQueue ( size_t sz ) {
q = wfq_create(sz);
}
inline int enq(Xclass *val) {
return wfq_enq(q, val);
}
inline Xclass *deq() {
return (Xclass*)wfq_deq(q);
}
inline size_t getSize() {
return wfq_size(q);
}
~MyQueue() {
wfq_destroy(q);;
}
}