kfifo
is a ring queue inspired by linux kernel. It is simple and efficient that can be used in embedded systems.
- Ultra Fast
- Lock Free
- Thread Safe (Single Producer With Single Consumer)
- Supports C99 And UP
kfifo.h should be dropped into an existing project and compiled along with it. The library provides some macros for using. Talking is cheap, show you a sample following:
#include <assert.h>
#include <stdlib.h>
#include "kfifo.h"
int main(void) {
_KFIFO(kfifo, int) kfifo;
KFIFO_INIT(&kfifo, 1000);
assert(KFIFO_CAPACITY(&kfifo) == 1023);
do {
int* n = calloc(1, sizeof(int));
*n = 1;
KFIFO_ENQUEUE(&kfifo, n);
} while(!KFIFO_FULL(&kfifo));
assert(KFIFO_LENGTH(&kfifo) == 1023);
do {
int* nn;
KFIFO_DEQUEUE(&kfifo, nn);
assert(*nn == 1);
free(nn);
} while(!KFIFO_EMPTY(&kfifo));
assert(KFIFO_LENGTH(&kfifo) == 0);
KFIFO_FREE(&kfifo);
return 0;
}