Multithreaded Programming Guide

Thread Pool Functions

The thr_pool.h header file declares the following functional interfaces.

thr_pool_create()

Creates a thread pool. More than one pool can be created.

typedef struct thr_pool thr_pool_t;	/* opaque to clients */

thr_pool_t *thr_pool_create(uint_t min_threads, uint_t max_threads,
                uint_t linger, pthread_attr_t *attr);
min_threads

Minimum number of threads in the pool.

max_threads

Maximum number of threads in the pool.

linger

Number of seconds that idle threads can linger before exiting, when no tasks come in. The idle threads can only exit if they are extra threads, above the number of minimum threads.

attr

Attributes of all worker threads. This can be NULL.

On error, thr_pool_create() returns NULL with errno set to the error code.

thr_pool_queue()

Enqueue a work request or task to the thread pool job queue.

int  thr_pool_queue(thr_pool_t *pool, void *(*func)(void *), void *arg);
pool

A thread pool identifier returned from thr_pool_create().

func

The task function to be called.

arg

The only argument passed to the task function.

On error, thr_pool_queue() returns -1 with errno set to the error code.

Notice the similarity of the func and arg arguments to the start_routine and arg arguments of pthread_create() shown in pthread_create Syntax. The thr_pool_queue() function can be used as a replacement for pthread_create() in existing applications. Note that if you use thr_pool_queue() instead of pthread_create(), you cannot use pthread_join() to wait for the task to complete.

thr_pool_wait()

Wait for all queued jobs to complete in the thread pool.

void  thr_pool_wait(thr_pool_t *pool);

pool is a thread pool identifier that is returned from thr_pool_create().

thr_pool_destroy()

Cancel all queued jobs and destroy the pool. Worker threads that are actively processing tasks are cancelled.

extern	void	thr_pool_destroy(thr_pool_t *pool);

pool is a thread pool identifier that is returned from thr_pool_create().