ftwin 0.8.10
napr_threadpool.h File Reference

A simple fixed-size thread pool for concurrent task processing. More...

#include <apr_pools.h>
Include dependency graph for napr_threadpool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct napr_threadpool_t napr_threadpool_t
 Opaque thread pool structure.
 
typedef apr_status_t() threadpool_process_data_callback_fn_t(void *ctx, void *data)
 The worker function executed by threads in the pool.
 

Functions

apr_status_t napr_threadpool_init (napr_threadpool_t **threadpool, void *ctx, unsigned long nb_thread, threadpool_process_data_callback_fn_t *process_data, apr_pool_t *pool)
 Initializes a thread pool.
 
apr_status_t napr_threadpool_add (napr_threadpool_t *threadpool, void *data)
 Adds a task (a data item) to the thread pool's processing queue.
 
apr_status_t napr_threadpool_wait (napr_threadpool_t *threadpool)
 Waits until all tasks currently in the queue have been processed.
 
apr_status_t napr_threadpool_shutdown (napr_threadpool_t *threadpool)
 Shuts down the thread pool.
 

Detailed Description

A simple fixed-size thread pool for concurrent task processing.

Definition in file napr_threadpool.h.

Typedef Documentation

◆ threadpool_process_data_callback_fn_t

typedef apr_status_t() threadpool_process_data_callback_fn_t(void *ctx, void *data)

The worker function executed by threads in the pool.

This function is called for each data item added to the pool's queue.

Parameters
[in]ctxA pointer to the shared context, provided during pool initialization.
[in]dataA pointer to the data item to be processed.
Returns
APR_SUCCESS on successful processing. Any other return value is considered an error.

Definition at line 41 of file napr_threadpool.h.

Function Documentation

◆ napr_threadpool_add()

apr_status_t napr_threadpool_add ( napr_threadpool_t *  threadpool,
void *  data 
)

Adds a task (a data item) to the thread pool's processing queue.

A waiting worker thread will pick up and process this item.

Parameters
[in]threadpoolThe thread pool.
[in]dataA pointer to the data item to be processed.
Returns
APR_SUCCESS on success, or an error code if the task could not be added.

Definition at line 245 of file napr_threadpool.c.

References DEBUG_ERR.

◆ napr_threadpool_init()

apr_status_t napr_threadpool_init ( napr_threadpool_t **  threadpool,
void *  ctx,
unsigned long  nb_thread,
threadpool_process_data_callback_fn_t process_data,
apr_pool_t *  pool 
)

Initializes a thread pool.

This creates a pool with a fixed number of worker threads that will process tasks from a queue.

Parameters
[out]threadpoolA pointer to a napr_threadpool_t* that will be allocated and initialized.
[in]ctxA user-defined context pointer that will be passed to the worker function.
[in]nb_threadThe number of worker threads to create in the pool.
[in]process_dataThe callback function that threads will execute to process tasks.
[in]poolThe APR pool to use for allocating the thread pool and its resources.
Returns
APR_SUCCESS on success, or an error code on failure.

Definition at line 200 of file napr_threadpool.c.

References DEBUG_ERR.

◆ napr_threadpool_shutdown()

apr_status_t napr_threadpool_shutdown ( napr_threadpool_t *  threadpool)

Shuts down the thread pool.

This function signals all worker threads to terminate, waits for them to finish their current tasks, and then joins them. No new tasks can be added after this

function is called.

Parameters
[in]threadpoolThe thread pool to shut down.
Returns
APR_SUCCESS on success.

Definition at line 390 of file napr_threadpool.c.

References DEBUG_ERR.

◆ napr_threadpool_wait()

apr_status_t napr_threadpool_wait ( napr_threadpool_t *  threadpool)

Waits until all tasks currently in the queue have been processed.

This function blocks until the number of pending tasks reaches zero.

Parameters
[in]threadpoolThe thread pool.
Returns
APR_SUCCESS on success.

Definition at line 268 of file napr_threadpool.c.

References DEBUG_ERR.