How to utilize a thread pool with pthreads?
The key to a thread pool is a queue. Here are modified functions for a thread pool I have developed.
Put element in queue
void queue_add(queue q, void *value)
{
pthread_mutex_lock(&q->mtx);
/* Add element normally. */
pthread_mutex_unlock(&q->mtx);
/* Signal waiting threads. */
pthread_cond_signal(&q->cond);
}
Get element from queue
void queue_get(queue q, void **val_r)
{
pthread_mutex_lock(&q->mtx);
/* Wait for element to become available. */
while (empty(q))
rc = pthread_cond_wait(&q->cond, &q->mtx);
/* We have an element. Pop it normally and return it in val_r. */
pthread_mutex_unlock(&q->mtx);
}
As an alternate riff on cnicutar's answer you can just use POSIX message queues which will take care of the synchronization concerns in the kernel. There will be some small overhead for the system calls which may or may not be a concern. It is pretty minimal as the kernel is doing everything you would have to do manually anyway.
The consumer threads can just block on mq_receive
and if you create a special type of queue message it makes it easy to tell the threads when to shut down.