c-queues is a small C queue library with a common API and multiple
implementations:
mutex_queuefor a simple mutex-protected ring bufferlf_spscfor single-producer/single-consumer uself_spmcfor single-producer/multi-consumer uself_mpscfor multi-producer/single-consumer uself_mpmcfor multi-producer/multi-consumer use
All queues store int values and use a fixed capacity. Capacities must be a
power of two.
The project uses CMake.
cmake -S . -B build
cmake --build buildThis builds the library code plus two executables:
testsbenchmarks
The test binary accepts one or more queue-type flags:
./build/tests/tests --spsc
./build/tests/tests --spmc
./build/tests/tests --mpsc
./build/tests/tests --mpmc
./build/tests/tests --spsc --spmc --mpsc --mpmcThe benchmark binary accepts one or more benchmark flags:
./build/benchmarks/benchmarks --mutex
./build/benchmarks/benchmarks --spsc
./build/benchmarks/benchmarks --spmc
./build/benchmarks/benchmarks --mpsc
./build/benchmarks/benchmarks --mpmc
./build/benchmarks/benchmarks --mutex --spsc --spmc --mpsc --mpmcEach queue implementation exposes create, push, pop, and destroy functions.
The generic wrapper in include/queue.h provides queue_enqueue,
queue_dequeue, and queue_destroy.
Example:
#include "lf_spsc.h"
#include "queue.h"
int main(void) {
queue_t *q = lf_spsc_create(1024);
if (!q) {
return 1;
}
queue_enqueue(q, 42);
int value = 0;
if (queue_dequeue(q, &value) == 0) {
/* value == 42 */
}
queue_destroy(q);
return 0;
}- Queue capacity must be non-zero and a power of two.
- The current API stores
intvalues. queue_destroyand the type-specific destroy functions free the queue instance and its storage; callers must ensure no other threads are still using the queue.