sci/include/threadpool.h

43 lines
1.1 KiB
C
Raw Normal View History

#ifndef SCI_THREADPOOL_H
#define SCI_THREADPOOL_H
2024-08-04 12:45:32 +02:00
#include "optional.h"
#include <pthread.h>
2024-08-04 12:45:32 +02:00
#include <stdbool.h>
// a work function
typedef void (*thread_func)(void *arg);
// linked list for work functions
typedef struct threadpool_work {
thread_func func;
void* arg;
struct threadpool_work* next;
} threadpool_work;
2024-08-04 12:45:32 +02:00
typedef optional_type(threadpool_work*) optional_threadpool_work;
// thread pool object
typedef struct {
threadpool_work* work_first;
threadpool_work* work_last;
pthread_mutex_t work_mutex;
pthread_cond_t work_cond;
pthread_cond_t working_cond;
size_t working_count;
size_t thread_count;
bool stop;
} threadpool;
// create a new threadpool instance with `num` amount of worker threads
threadpool* threadpool_create(size_t num);
// destroy a threadpool instance
void threadpool_destroy(threadpool* pool);
// add work to the threadpool
bool threadpool_add_work(threadpool* pool, thread_func func, void* arg);
// wait for the remaining work in the threadpool to complete
void threadpool_wait(threadpool* pool);
#endif