MPMT v1.7.4
thread_pool.h File Reference

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_TThreadPool
 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)
 

Detailed Description

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.


Data Structure Documentation

◆ ThreadPoolTask

struct ThreadPoolTask

Thread pool task structure.

Data Fields
void(*)(void *) function
void * argument

Function Documentation

◆ createThreadPool()

ThreadPool createThreadPool ( size_t threadCount,
size_t taskCapacity,
TaskOrder taskOrder )

Creates a new thread pool instance.

Note
You should destroy created thread pool instance manually.

Internally it allocates the necessary mutexes, condwars and arrays. It also creates and starts the specified number of threads.

Parameters
threadCounttarget thread count in the pool
taskCapacitytask buffer size
taskOrdertask order type
Returns
Thread pool instance on success, otherwise NULL.

◆ destroyThreadPool()

void destroyThreadPool ( ThreadPool threadPool)

Destroys thread pool instance. (Blocking)

Parameters
threadPoolthread pool instance or NULL

◆ getThreadPoolThreadCount()

size_t getThreadPoolThreadCount ( ThreadPool threadPool)

Returns thread pool thread count.

Parameters
threadPoolthread pool instance

◆ getThreadPoolTaskCapacity()

size_t getThreadPoolTaskCapacity ( ThreadPool threadPool)

Returns thread pool task capacity.

Parameters
threadPoolthread pool instance

◆ isThreadPoolRunning()

bool isThreadPoolRunning ( ThreadPool threadPool)

Returns true if any thread is running task.

Parameters
threadPoolthread pool instance

◆ getThreadPoolTaskOrder()

TaskOrder getThreadPoolTaskOrder ( ThreadPool threadPool)

Returns thread pool task order type.

Parameters
threadPoolthread pool instance

◆ setThreadPoolTaskOrder()

void setThreadPoolTaskOrder ( ThreadPool threadPool,
TaskOrder taskOrder )

Sets thread pool task order type. (Blocking)

Parameters
threadPoolthread pool instance
taskOrdertask order type

◆ resizeThreadPoolTasks()

bool resizeThreadPoolTasks ( ThreadPool threadPool,
size_t taskCapacity )

Resize thread pool task buffer. (Blocking)

Parameters
threadPoolthread pool instance
taskCapacitytask buffer size
Returns
True on success, otherwise false.

◆ tryAddThreadPoolTask()

bool tryAddThreadPoolTask ( ThreadPool threadPool,
ThreadPoolTask task )

Adds a new task to the thread pool, if enough space.

Parameters
threadPoolthread pool instance
tasktarget thread pool task
Returns
True if task successfully added, otherwise false.

◆ addThreadPoolTask()

void addThreadPoolTask ( ThreadPool threadPool,
ThreadPoolTask task )

Adds a new task to the thread pool. (Blocking)

Parameters
threadPoolthread pool instance
tasktarget thread pool task

◆ addThreadPoolTasks()

void addThreadPoolTasks ( ThreadPool threadPool,
ThreadPoolTask * tasks,
size_t taskCount )

Adds a new tasks to the thread pool. (Blocking)

Parameters
threadPoolthread pool instance
[in,out]taskstarget thread pool tasks
taskCounttask array size

◆ addThreadPoolTaskNumber()

void addThreadPoolTaskNumber ( ThreadPool threadPool,
ThreadPoolTask task,
size_t taskCount )

Adds a new tasks to the thread pool. (Blocking)

Parameters
threadPoolthread pool instance.
tasktarget thread pool task.
taskCounttask count

◆ waitThreadPool()

void waitThreadPool ( ThreadPool threadPool)

Waits until the thread pool has completed all tasks. (Blocking)

Parameters
threadPoolthread pool instance.