Refer to the online implementation of the next C thread pool
Code Update: Https://github.com/ljfly/pool
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include < pthread.h> #include <assert.h>typedef struct worker {void * (*process) (void *arg); void *arg; struct worker *next;} cthread_worker;typedef struct {pthread_mutex_t queue_lock; pthread_cond_t Queue_ready; Cthread_worker *queue_head; int shutdown; pthread_t *threadid; int max_thread_num; int cur_queue_size;} Cthread_pool;int Pool_add_worker (void* (*process) (void *arg), void *arg); void *thread_routine (void *arg); static Cthread_pool *pool = null;void pool_init (int max_thread_num) {pool = (Cthread_pool *) malloc (sizeof (Cthread_pool)); Pthread_mutex_init (& (Pool->queue_lock), NULL); Pthread_cond_init (& (Pool->queue_ready), NULL); Pool->queue_head = NULL; Pool->max_thread_num = Max_thread_num; pool->cur_queue_size = 0; Pool->shutdown = 0; Pool->threadid = (pthread_t *) malloc (Max_threAd_num * sizeof (pthread_t)); int i = 0; for (int i = 0; i < max_thread_num; i++) pthread_create (& (Pool->threadid[i]), null,thread_routine,null); }int Pool_add_worker (void * (*process) (void *arg), void *arg) {Cthread_worker *newworker = (Cthread_worker *) malloc (sizeof (Cthread_worker)); newworker->process = process; Newworker->arg = arg; newworker->next; Pthread_mutex_lock (& (Pool->queue_lock)); Cthread_worker *member = pool->queue_head; if (member! = NULL) {while (member->next! = null) member = member->next; Member->next = Newworker; } else Pool->queue_head = Newworker; ASSERT (Pool->queue_head! = NULL); pool->cur_queue_size++; Pthread_mutex_unlock (& (Pool->queue_lock)); Pthread_cond_signal (& (Pool->queue_ready)); return 0;} int Pool_destroy () {if (pool->shutdown) return-1; Pool->shutdown = 1; Pthread_cond_broadcast (& (Pool->queue_ready)); int i; for (i = 0; i < pool->max_thread_num; i++) Pthread_join (pool->threadid[i],null); Free (pool->threadid); Cthread_worker *head = NULL; while (pool->queue_head! = NULL) {head = pool->queue_head; Pool->queue_head = pool->queue_head->next; Free (head); } Pthread_mutex_destroy (& (Pool->queue_lock)); Pthread_cond_destroy (& (Pool->queue_ready)); Free (pool); Pool=null; return 0; } void * Thread_routine (void *arg) {printf ("startting thread 0x%x\n", pthread_self ()); while (1) {Pthread_mutex_lock (& (Pool->queue_lock)); while (pool->cur_queue_size = = 0 &&!pool->shutdown) {printf ("Thread 0x%x is waitting\n", pthr Ead_self ()); Pthread_cond_wait (& (Pool->queue_ready),& (Pool->queue_lock)); } if (Pool->shutdown) {Pthread_muteX_unlock (& (Pool->queue_lock)); printf ("Thread 0x%x would exit\n", pthread_self ()); Pthread_exit (NULL); } printf ("Thread 0x%x is starting to work\n", pthread_self ()); ASSERT (Pool->cur_queue_size! = 0); ASSERT (Pool->queue_head! = NULL); pool->cur_queue_size--; Cthread_worker *worker = pool->queue_head; Pool->queue_head = worker->next; Pthread_mutex_unlock (& (Pool->queue_lock)); (* (worker->process)) (Worker->arg); Free (worker); worker = NULL; } pthread_exit (NULL); } void *myprocess (Void*arg) {printf ("ThreadID is 0x%x,working on task%d\n", Pthread_self (), * (int*) arg); Sleep (1); return NULL; } int main (int argc, char *argv[]) {int threadnum, tacknum; printf ("Please enter the count of thread and the number of tack\n"); scanf ("%d%d",&threadnum,&Tacknum); Pool_init (threadnum); int *workingnum = (int*) malloc (sizeof (int) *tacknum); int i; for (i = 0; i < Tacknum; i++) {workingnum[i] = i; Pool_add_worker (Myprocess,&workingnum[i]); } sleep (5); Pool_destroy (); Free (workingnum); return 0; }
Thread pool for Linux under C