The thread pool uses POSIX threads, so compile using the -lpthread
flag.
gcc example.c thrpl.c -lpthread -o example && ./example
Debug mode:
gcc example.c thrpl.c -lpthread -DTHRPL_DEBUG -o example && ./example
git clone https://github.com/zaiic/thrpl.c
cd thrpl.c
Put thrpl.c
and thrpl.h
in your project or generate a header-only file and put it in your project.
Generate header-only file:
./generate_header_only.sh
Notice: The header-only file is stb style.
Don't forget to put #define THRPL_IMLEMENTATION
in your file before including header-only file.
#define THRPL_IMLEMENTATION
#include "thrpl.h" // header-only file
Function | Description |
---|---|
ThreadPool *ThreadPool_new() | Create a new thread pool and return a pointer to it. |
int ThreadPool_add_task(ThreadPool *self, Task task) | Add a task to the thread pool. |
int ThreadPool_gracefully_destroy(ThreadPool *self) | Destroy the thread pool when the task queue is empty. |
int ThreadPool_destroy(ThreadPool *self) | Destroy the thread pool. |
typedef void *(*function)(void *);
typedef struct {
function func;
void *argv;
} Task;