Simple task queue implementation in C that allows to add tasks to a queue and execute them from multiple threads.
Currently, only pthreads are supported.
git clone git@github.com:RaphiaRa/task_queue.git
mkdir task_queue/build; cd task_queue/build
cmake ..
make
make test
Either...
- copy
tq.h
andtq.c
to your project and compile them with your project - or use CMake's
add_subdirectory
to add thetask_queue
subdirectory to your project, then link againsttask_queue::task_queue
#include "tq.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct count_task {
tq_task base;
tq_runner* runner;
int count;
} count_task;
static void count_task_fn(void* self)
{
count_task* task = self;
printf("Task count: %d\n", ++task->count);
if (task->count < 100)
tq_runner_push(task->runner, &task->base);
}
int main()
{
tq_runner* runner;
tq_runner_create(&runner);
count_task task = { 0 };
task.base.fn = count_task_fn;
task.runner = runner;
tq_runner_push(runner, &task.base);
tq_runner_run(runner);
tq_runner_destroy(runner);
return 0;
}
To execute tasks in parallel, simply call tq_runner_run
in multiple threads.
static void* run_rountine(void* arg)
{
tq_runner* runner = arg;
tq_runner_run(runner);
return NULL;
}
.
.
.
pthread_t threads[100];
for (int i = 0; i < 100; ++i)
pthread_create(&threads[i], NULL, run_rountine, runner);
for (int i = 0; i < 100; ++i)
pthread_join(threads[i], NULL);