Definition of thread pool
The thread pool is a form of multithreading that adds tasks to the queue during processing, and then automatically starts those tasks after the thread is created. Thread pool threads are all background threads. Each thread uses the default stack size, runs at the default priority, and is in a multithreaded apartment. If a thread is idle in managed code, such as waiting for an event, the thread pool inserts another worker thread to keep all the processors busy. If all thread pool threads are always busy, but the queue contains pending work, the thread pool will create another worker thread after a period of time but the number of threads will never exceed the maximum value. Threads that exceed the maximum value can be queued, but they will not start until other threads have finished.
When do I need to create a thread pool?
Simply put, if an application needs to create and destroy threads frequently, and the task executes in a very short time, the overhead of creating and destroying threads is not negligible, and this is the opportunity for the thread pool to appear. If the thread creation and destruction times are negligible compared to the execution time of the task, there is no need to use the thread pool.
Implementation program (release of related resources not completed)
/******************** Pthread Pool 14/06/18 22:06 ********************/#include <stdio.h> #include <stdli b.h> #include <unistd.h> #include <sys/types.h> #include <pthread.h> #include <pthread.h>/* The task node structure */typedef struct Node {void (*func) (void *arg); unsigned int *work_id; struct node *next;} tasknode;/* thread pool structure */typedef struct {pthread_cond_t ready; pthread_mutex_t lock; struct node *task_head; pthread_t *thread_id; unsigned int cur_queue_size;} pthreadpool;static Pthreadpool *pool = null;/* define thread */void *thread_routine (void *arg) {while (1) {PTHREAD_MU Tex_lock (&pool->lock); if (!pool->cur_queue_size) {printf ("Thread is waiting .... \ n"); Pthread_cond_wait (&pool->ready, &pool->lock); } pool->cur_queue_size--; Tasknode *worker = pool->task_head; Pool->task_head= worker->next; PThread_mutex_unlock (& (Pool->lock)); /* Call function, perform task */Worker->func (worker->work_id); Free (worker); worker = NULL; }}static void System_init () {int i = 0; Pool = (pthreadpool*) malloc (sizeof (Pthreadpool)); /* Initialize lock and condition variable */pthread_mutex_init (&pool->lock, NULL); Pthread_cond_init (&pool->ready, NULL); /* Add 3 threads to the pool */pool->thread_id = (pthread_t*) malloc (3 * sizeof (Pthreadpool)); pool->cur_queue_size = 0; Pool->task_head= NULL; for (; i<3; i++) pthread_create (&pool->thread_id[i], NULL, thread_routine, NULL);} /* Add Task */int pool_add_work (void (*process) (void *arg), unsigned int *id) {Tasknode *new_work = (tasknode*) malloc (siz EOF (Tasknode)); New_work->func = process; new_work->work_id = ID; New_work->next = NULL; pool->cur_queue_size++; Tasknode *temp = pool->task_head; if (!temp) Pool->task_head =New_work; else{while (temp->next) temp = temp->next; Temp->next = New_work; } pthread_cond_signal (&pool->ready);} /* Define User Task */void my_process (void *arg) {printf ("ThreadID is 0x%x, working on task%d \ n", pthread_self (), * (int *) arg) ; Sleep (1);} int main () {unsigned int work_id[10]; int i = 0; System_init (); /* Wait for thread to run */sleep (1); for (; i<10; i++) {Work_id[i] = i; Pool_add_work (My_process, &work_id[i]); }/* Wait for the task to finish */sleep (5); return 0;}
After compiling on Linux, run the result:
Linux C implements a simple thread pool