libtpool.a A general purpose thread poll library
To build, just run:
tar xvf libtpool.tar
make
Pthreads are mandatory and the build requires that pthread.h exist at
the system level.
To use:
-------------------------------------------------------------------------
Start up a thread pool:
-------------------------------------------------------------------------
#include "tpool.h"
main()
{
TPOOL_CTX * tpool_ctx = NULL;
char buf[BUFSIZ];
int num_threads = 100;
tpool_ctx = tpool_init(num_threads, ebuf, sizeof ebuf);
if (tpool_ctx == NULL)
handle_error_here;
-------------------------------------------------------------------------
Shutdown the thread pool:
-------------------------------------------------------------------------
if (tpool_ctx != NULL)
tpool_ctx = tpool_shutdown(tpool_ctx, ebuf, sizeof ebuf);
-------------------------------------------------------------------------
To use the threads, first create a function to be run by a thread:
-------------------------------------------------------------------------
void *
function_to_run(void *arg, int *state)
{
// data passed in arg
// if *state == TPOOL_WORKER_STATE_EXIT the thread pool is shutting down
}
-------------------------------------------------------------------------
Optionally, either pass NULL or create a custom function to free your
data when the thread finishes
-------------------------------------------------------------------------
void
function_to_free(void *)
{
}
-------------------------------------------------------------------------
Launch a thread to run the function_to_run(). This call starts up an idle
thread and runs the function using that thread. This function returns
immediately while the thread runs asynchronously.
-------------------------------------------------------------------------
int ret;
char data[64]; // for example
ret = bm_tpool_push_task(tpool_ctx, function_to_run, function_to_free, (void *)data, ebuf, sizeof ebuf);
if (ret != 0)
handle_error;
-------------------------------------------------------------------------
Debugging prints to standard out.
Use 1 to turn on debugging, 0 to turn it off:
-------------------------------------------------------------------------
(void) tpool_debug_set(1); /* turn on */
(void) tpool_debug_set(0); /* turn off */
-------------------------------------------------------------------------
Find out how many worker threads are busy, or increase the pool size
by a fixed amount. If delta is 0 just fetch the count. If delta is
greater than 0, both increase the pool size and return the new count.
-------------------------------------------------------------------------
int count;
int delta = 25;
count = tpool_delta(tpool_ctx, delta);
if (count == 0)
handle_error_here;