https://github.com/raphiara/task_queue
Simple C (99) task queue library
https://github.com/raphiara/task_queue
c c99 library multithreading parallel queue task task-queue threading work-queue
Last synced: 2 months ago
JSON representation
Simple C (99) task queue library
- Host: GitHub
- URL: https://github.com/raphiara/task_queue
- Owner: RaphiaRa
- License: mit
- Created: 2023-09-13T11:50:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-25T17:26:03.000Z (over 1 year ago)
- Last Synced: 2024-09-29T01:20:59.357Z (8 months ago)
- Topics: c, c99, library, multithreading, parallel, queue, task, task-queue, threading, work-queue
- Language: C
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## C Task Queue
#### Description
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.
#### Build & run tests
```sh
git clone [email protected]:RaphiaRa/task_queue.git
mkdir task_queue/build; cd task_queue/build
cmake ..
make
make test
```#### Install
Either...
- copy `tq.h` and `tq.c` to your project and compile them with your project
- or use CMake's `add_subdirectory` to add the `task_queue` subdirectory to your project, then link against `task_queue::task_queue`#### Example
```c
#include "tq.h"#include
#includetypedef 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;
}```
#### Parallel execution
To execute tasks in parallel, simply call `tq_runner_run` in multiple threads.
```c
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);
```