Thread pool functions. More...
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
Data Structures | |
struct | ThreadPoolTask |
Thread pool task structure. More... | |
Typedefs | |
typedef uint8_t | TaskOrder |
Task order type. | |
typedef struct ThreadPool_T | ThreadPool_T |
Thread pool structure. | |
typedef ThreadPool_T * | ThreadPool |
Thread pool instance. | |
Enumerations | |
enum | TaskOrder_T { STACK_TASK_ORDER = 0 , QUEUE_TASK_ORDER = 1 , TASK_ORDER_COUNT = 2 } |
Task order types. | |
Functions | |
ThreadPool | createThreadPool (size_t threadCount, size_t taskCapacity, TaskOrder taskOrder) |
Creates a new thread pool instance. | |
void | destroyThreadPool (ThreadPool threadPool) |
Destroys thread pool instance. (Blocking) | |
size_t | getThreadPoolThreadCount (ThreadPool threadPool) |
Returns thread pool thread count. | |
size_t | getThreadPoolTaskCapacity (ThreadPool threadPool) |
Returns thread pool task capacity. | |
bool | isThreadPoolRunning (ThreadPool threadPool) |
Returns true if any thread is running task. | |
TaskOrder | getThreadPoolTaskOrder (ThreadPool threadPool) |
Returns thread pool task order type. | |
void | setThreadPoolTaskOrder (ThreadPool threadPool, TaskOrder taskOrder) |
Sets thread pool task order type. (Blocking) | |
bool | resizeThreadPoolTasks (ThreadPool threadPool, size_t taskCapacity) |
Resize thread pool task buffer. (Blocking) | |
bool | tryAddThreadPoolTask (ThreadPool threadPool, ThreadPoolTask task) |
Adds a new task to the thread pool, if enough space. | |
void | addThreadPoolTask (ThreadPool threadPool, ThreadPoolTask task) |
Adds a new task to the thread pool. (Blocking) | |
void | addThreadPoolTasks (ThreadPool threadPool, ThreadPoolTask *tasks, size_t taskCount) |
Adds a new tasks to the thread pool. (Blocking) | |
void | addThreadPoolTaskNumber (ThreadPool threadPool, ThreadPoolTask task, size_t taskCount) |
Adds a new tasks to the thread pool. (Blocking) | |
void | waitThreadPool (ThreadPool threadPool) |
Waits until the thread pool has completed all tasks. (Blocking) | |
Thread pool functions.
A thread pool is a programming concept and design pattern used in concurrent programming to efficiently manage and reuse a pool of worker threads. Instead of creating a new thread for each task, a thread pool maintains a fixed number of threads that are created and initialized upfront, ready to execute tasks concurrently. This helps to reduce the overhead associated with creating and destroying threads for every individual task, resulting in better performance and resource utilization.
ThreadPool createThreadPool | ( | size_t | threadCount, |
size_t | taskCapacity, | ||
TaskOrder | taskOrder ) |
Creates a new thread pool instance.
Internally it allocates the necessary mutexes, condwars and arrays. It also creates and starts the specified number of threads.
threadCount | target thread count in the pool |
taskCapacity | task buffer size |
taskOrder | task order type |
void destroyThreadPool | ( | ThreadPool | threadPool | ) |
Destroys thread pool instance. (Blocking)
threadPool | thread pool instance or NULL |
size_t getThreadPoolThreadCount | ( | ThreadPool | threadPool | ) |
Returns thread pool thread count.
threadPool | thread pool instance |
size_t getThreadPoolTaskCapacity | ( | ThreadPool | threadPool | ) |
Returns thread pool task capacity.
threadPool | thread pool instance |
bool isThreadPoolRunning | ( | ThreadPool | threadPool | ) |
Returns true if any thread is running task.
threadPool | thread pool instance |
TaskOrder getThreadPoolTaskOrder | ( | ThreadPool | threadPool | ) |
Returns thread pool task order type.
threadPool | thread pool instance |
void setThreadPoolTaskOrder | ( | ThreadPool | threadPool, |
TaskOrder | taskOrder ) |
Sets thread pool task order type. (Blocking)
threadPool | thread pool instance |
taskOrder | task order type |
bool resizeThreadPoolTasks | ( | ThreadPool | threadPool, |
size_t | taskCapacity ) |
Resize thread pool task buffer. (Blocking)
threadPool | thread pool instance |
taskCapacity | task buffer size |
bool tryAddThreadPoolTask | ( | ThreadPool | threadPool, |
ThreadPoolTask | task ) |
Adds a new task to the thread pool, if enough space.
threadPool | thread pool instance |
task | target thread pool task |
void addThreadPoolTask | ( | ThreadPool | threadPool, |
ThreadPoolTask | task ) |
Adds a new task to the thread pool. (Blocking)
threadPool | thread pool instance |
task | target thread pool task |
void addThreadPoolTasks | ( | ThreadPool | threadPool, |
ThreadPoolTask * | tasks, | ||
size_t | taskCount ) |
Adds a new tasks to the thread pool. (Blocking)
threadPool | thread pool instance | |
[in,out] | tasks | target thread pool tasks |
taskCount | task array size |
void addThreadPoolTaskNumber | ( | ThreadPool | threadPool, |
ThreadPoolTask | task, | ||
size_t | taskCount ) |
Adds a new tasks to the thread pool. (Blocking)
threadPool | thread pool instance. |
task | target thread pool task. |
taskCount | task count |
void waitThreadPool | ( | ThreadPool | threadPool | ) |
Waits until the thread pool has completed all tasks. (Blocking)
threadPool | thread pool instance. |