Queue
A thread safe type-agnostic header-only macro-based struct queue implementation in C.
Install / Use
/learn @alxyng/QueueREADME
queue
A thread safe type-agnostic header-only macro-based structure queue implementation in C.
Usage
Include the queue.h header file in your source. To create a queueable
structure, include the queue_handle qh member in the struct definition.
Before any other operations, ensure that QUEUE_INIT has been called, it is an error to not do so. This initialises the queue and sets up the mutex and conditions variables. Setting the queue to NULL will protect against errors if a queue has not been
Freeing the queue does not free every element in the queue if they have been dynamically allocated. This has to be done by popping all the elements in the queue and freeing them manually.
Pushing the same element (at the same address) into the queue is not supported
and is an error. This is because the next member of the queue's queue_handle
will be overwritten. This will cause undefined behavior when pushing or popping
to or from the queue (see commented out test case).
#include <stdlib.h>
#include <stdio.h>
#include "queue.h"
struct msg {
char *content;
queue_handle qh;
};
int main(void) {
struct msg *msgs; // message queue
struct msg m1, *m2;
QUEUE_INIT(struct msg, msgs);
m1.content = "abc";
QUEUE_PUSH(msgs, &m1);
printf("msgs size: %d\n", QUEUE_SIZE(msgs)); // msgs size: 1
QUEUE_POP(msgs, m2);
printf("m2 content: %s\n", m2->content); // m2 content: abc
QUEUE_FREE(msgs);
return EXIT_SUCCESS;
}
Testing and building examples
To run tests run
make check
To compile the examples run
make examples
Both of the above can be done together by running
make
Related Skills
node-connect
350.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
350.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
350.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
