線程池代碼
阿新 • • 發佈:2018-05-25
thread ram 目的 efi 狀態 return 檢測 adjust nag
頭文件head.h
#ifndef __THREADPOOL_H_ #define __THREADPOOL_H_ typedef struct threadpool_t threadpool_t; /** * @function threadpool_create * @descCreates a threadpool_t object. * @param thr_num thread num * @param max_thr_num max thread size * @param queue_max_size size of the queue. * @return a newly created thread pool or NULL */ threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size); /** * @function threadpool_add * @desc add a new task in the queue of a thread pool * @param pool Thread pool to which add the task. * @param function Pointer to the function that will perform the task. * @param argument Argument to be passed to the function. * @return 0 if all goes well,else -1 */ int threadpool_add(threadpool_t *pool, void*(*function)(void *arg), void *arg); /** * @function threadpool_destroy * @desc Stops and destroys a thread pool. * @param pool Thread pool to destroy. * @return 0 if destory success else -1 */ int threadpool_destroy(threadpool_t *pool); /** * @desc get the thread num * @pool pool threadpool * @return # of the thread */ int threadpool_all_threadnum(threadpool_t *pool); /** * desc get the busy thread num * @param pool threadpool * return # of the busy thread */ int threadpool_busy_threadnum(threadpool_t *pool); #endif
main.c
#include <stdlib.h> #include <pthread.h> #include <unistd.h> #include <assert.h> #include <stdio.h> #include <string.h> #include <signal.h> #include <errno.h> #include "threadpool.h" #define DEFAULT_TIME 10 /*10s檢測一次*/ #define MIN_WAIT_TASK_NUM 10 /*如果queue_size > MIN_WAIT_TASK_NUM 添加新的線程到線程池*/ #define DEFAULT_THREAD_VARY 10 /*每次創建和銷毀線程的個數*/ #define true 1 #define false 0 typedef struct { void *(*function)(void *); /* 函數指針,回調函數 */ void *arg; /* 上面函數的參數 */ } threadpool_task_t; /* 各子線程任務結構體 */ /* 描述線程池相關信息 */ struct threadpool_t { pthread_mutex_t lock; /* 用於鎖住本結構體 */ pthread_mutex_t thread_counter; /* 記錄忙狀態線程個數de瑣 -- busy_thr_num */ pthread_cond_t queue_not_full; /* 當任務隊列滿時,添加任務的線程阻塞,等待此條件變量 */ pthread_cond_t queue_not_empty; /* 任務隊列裏不為空時,通知等待任務的線程 */ pthread_t *threads; /* 存放線程池中每個線程的tid。數組 */ pthread_t adjust_tid; /* 存管理線程tid */ threadpool_task_t *task_queue; /* 任務隊列 */ int min_thr_num; /* 線程池最小線程數 */ int max_thr_num; /* 線程池最大線程數 */ int live_thr_num; /* 當前存活線程個數 */ int busy_thr_num; /* 忙狀態線程個數 */ int wait_exit_thr_num; /* 要銷毀的線程個數 */ int queue_front; /* task_queue隊頭下標 */ int queue_rear; /* task_queue隊尾下標 */ int queue_size; /* task_queue隊中實際任務數 */ int queue_max_size; /* task_queue隊列可容納任務數上限 */ int shutdown; /* 標誌位,線程池使用狀態,true或false */ }; /** * @function void *threadpool_thread(void *threadpool) * @desc the worker thread * @param threadpool the pool which own the thread */ void *threadpool_thread(void *threadpool); /** * @function void *adjust_thread(void *threadpool); * @desc manager thread * @param threadpool the threadpool */ void *adjust_thread(void *threadpool); /** * check a thread is alive */ int is_thread_alive(pthread_t tid); int threadpool_free(threadpool_t *pool); threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size) { int i; threadpool_t *pool = NULL; do { if((pool = (threadpool_t *)malloc(sizeof(threadpool_t))) == NULL) { printf("malloc threadpool fail"); break;/*跳出do while*/ } pool->min_thr_num = min_thr_num; pool->max_thr_num = max_thr_num; pool->busy_thr_num = 0; pool->live_thr_num = min_thr_num; /* 活著的線程數 初值=最小線程數 */ pool->queue_size = 0; /* 有0個產品 */ pool->queue_max_size = queue_max_size; pool->queue_front = 0; pool->queue_rear = 0; pool->shutdown = false; /* 不關閉線程池 */ /* 根據最大線程上限數, 給工作線程數組開辟空間, 並清零 */ pool->threads = (pthread_t *)malloc(sizeof(pthread_t)*max_thr_num); if (pool->threads == NULL) { printf("malloc threads fail"); break; } memset(pool->threads, 0, sizeof(pthread_t)*max_thr_num); /* 隊列開辟空間 */ pool->task_queue = (threadpool_task_t *)malloc(sizeof(threadpool_task_t)*queue_max_size); if (pool->task_queue == NULL) { printf("malloc task_queue fail"); break; } /* 初始化互斥瑣、條件變量 */ if (pthread_mutex_init(&(pool->lock), NULL) != 0 || pthread_mutex_init(&(pool->thread_counter), NULL) != 0 || pthread_cond_init(&(pool->queue_not_empty), NULL) != 0 || pthread_cond_init(&(pool->queue_not_full), NULL) != 0) { printf("init the lock or cond fail"); break; } /* 啟動 min_thr_num 個 work thread */ for (i = 0; i < min_thr_num; i++) { pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool);/*pool指向當前線程池*/ printf("start thread 0x%x...\n", (unsigned int)pool->threads[i]); } pthread_create(&(pool->adjust_tid), NULL, adjust_thread, (void *)pool);/* 啟動管理者線程 */ return pool; } while (0); threadpool_free(pool); /* 前面代碼調用失敗時,釋放poll存儲空間 */ return NULL; } /* 向線程池中 添加一個任務 */ int threadpool_add(threadpool_t *pool, void*(*function)(void *arg), void *arg) { pthread_mutex_lock(&(pool->lock)); /* ==為真,隊列已經滿, 調wait阻塞 */ while ((pool->queue_size == pool->queue_max_size) && (!pool->shutdown)) { pthread_cond_wait(&(pool->queue_not_full), &(pool->lock)); } if (pool->shutdown) { pthread_mutex_unlock(&(pool->lock)); } /* 清空 工作線程 調用的回調函數 的參數arg */ if (pool->task_queue[pool->queue_rear].arg != NULL) { free(pool->task_queue[pool->queue_rear].arg); pool->task_queue[pool->queue_rear].arg = NULL; } /*添加任務到任務隊列裏*/ pool->task_queue[pool->queue_rear].function = function; pool->task_queue[pool->queue_rear].arg = arg; pool->queue_rear = (pool->queue_rear + 1) % pool->queue_max_size; /* 隊尾指針移動, 模擬環形 */ pool->queue_size++; /*添加完任務後,隊列不為空,喚醒線程池中 等待處理任務的線程*/ pthread_cond_signal(&(pool->queue_not_empty)); pthread_mutex_unlock(&(pool->lock)); return 0; } /* 線程池中各個工作線程 */ void *threadpool_thread(void *threadpool) { threadpool_t *pool = (threadpool_t *)threadpool; threadpool_task_t task; while (true) { /* Lock must be taken to wait on conditional variable */ /*剛創建出線程,等待任務隊列裏有任務,否則阻塞等待任務隊列裏有任務後再喚醒接收任務*/ pthread_mutex_lock(&(pool->lock)); /*queue_size == 0 說明沒有任務,調 wait 阻塞在條件變量上, 若有任務,跳過該while*/ while ((pool->queue_size == 0) && (!pool->shutdown)) { printf("thread 0x%x is waiting\n", (unsigned int)pthread_self()); pthread_cond_wait(&(pool->queue_not_empty), &(pool->lock)); /*清除指定數目的空閑線程,如果要結束的線程個數大於0,結束線程*/ if (pool->wait_exit_thr_num > 0) { pool->wait_exit_thr_num--; /*如果線程池裏線程個數大於最小值時可以結束當前線程*/ if (pool->live_thr_num > pool->min_thr_num) { printf("thread 0x%x is exiting\n", (unsigned int)pthread_self()); pool->live_thr_num--; pthread_mutex_unlock(&(pool->lock)); pthread_exit(NULL); } } } /*如果指定了true,要關閉線程池裏的每個線程,自行退出處理*/ if (pool->shutdown) { pthread_mutex_unlock(&(pool->lock)); printf("thread 0x%x is exiting\n", (unsigned int)pthread_self()); pthread_exit(NULL); /* 線程自行結束 */ } /*從任務隊列裏獲取任務, 是一個出隊操作*/ task.function = pool->task_queue[pool->queue_front].function; task.arg = pool->task_queue[pool->queue_front].arg; pool->queue_front = (pool->queue_front + 1) % pool->queue_max_size; /* 出隊,模擬環形隊列 */ pool->queue_size--; /*通知可以有新的任務添加進來*/ pthread_cond_broadcast(&(pool->queue_not_full)); /*任務取出後,立即將 線程池瑣 釋放*/ pthread_mutex_unlock(&(pool->lock)); /*執行任務*/ printf("thread 0x%x start working\n", (unsigned int)pthread_self()); pthread_mutex_lock(&(pool->thread_counter)); /*忙狀態線程數變量瑣*/ pool->busy_thr_num++; /*忙狀態線程數+1*/ pthread_mutex_unlock(&(pool->thread_counter)); (*(task.function))(task.arg); /*執行回調函數任務*/ //task.function(task.arg); /*執行回調函數任務*/ /*任務結束處理*/ printf("thread 0x%x end working\n", (unsigned int)pthread_self()); pthread_mutex_lock(&(pool->thread_counter)); pool->busy_thr_num--; /*處理掉一個任務,忙狀態數線程數-1*/ pthread_mutex_unlock(&(pool->thread_counter)); } pthread_exit(NULL); } /* 管理線程 */ void *adjust_thread(void *threadpool) { int i; threadpool_t *pool = (threadpool_t *)threadpool; while (!pool->shutdown) { sleep(DEFAULT_TIME); /*定時 對線程池管理*/ pthread_mutex_lock(&(pool->lock)); int queue_size = pool->queue_size; /* 關註 任務數 */ int live_thr_num = pool->live_thr_num; /* 存活 線程數 */ pthread_mutex_unlock(&(pool->lock)); pthread_mutex_lock(&(pool->thread_counter)); int busy_thr_num = pool->busy_thr_num; /* 忙著的線程數 */ pthread_mutex_unlock(&(pool->thread_counter)); /* 創建新線程 算法: 任務數大於最小線程池個數, 且存活的線程數少於最大線程個數時 如:30>=10 && 40<100*/ if (queue_size >= MIN_WAIT_TASK_NUM && live_thr_num < pool->max_thr_num) { pthread_mutex_lock(&(pool->lock)); int add = 0; /*一次增加 DEFAULT_THREAD 個線程*/ for (i = 0; i < pool->max_thr_num && add < DEFAULT_THREAD_VARY && pool->live_thr_num < pool->max_thr_num; i++) { if (pool->threads[i] == 0 || !is_thread_alive(pool->threads[i])) { pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool); add++; pool->live_thr_num++; } } pthread_mutex_unlock(&(pool->lock)); } /* 銷毀多余的空閑線程 算法:忙線程X2 小於 存活的線程數 且 存活的線程數 大於 最小線程數時*/ if ((busy_thr_num * 2) < live_thr_num && live_thr_num > pool->min_thr_num) { /* 一次銷毀DEFAULT_THREAD個線程, 隨機10個即可 */ pthread_mutex_lock(&(pool->lock)); pool->wait_exit_thr_num = DEFAULT_THREAD_VARY; /* 要銷毀的線程數 設置為10 */ pthread_mutex_unlock(&(pool->lock)); for (i = 0; i < DEFAULT_THREAD_VARY; i++) { /* 通知處在空閑狀態的線程, 他們會自行終止*/ pthread_cond_signal(&(pool->queue_not_empty)); } } } return NULL; } int threadpool_destroy(threadpool_t *pool) { int i; if (pool == NULL) { return -1; } pool->shutdown = true; /*先銷毀管理線程*/ pthread_join(pool->adjust_tid, NULL); for (i = 0; i < pool->live_thr_num; i++) { /*通知所有的空閑線程*/ pthread_cond_broadcast(&(pool->queue_not_empty)); } for (i = 0; i < pool->live_thr_num; i++) { pthread_join(pool->threads[i], NULL); } threadpool_free(pool); return 0; } int threadpool_free(threadpool_t *pool) { if (pool == NULL) { return -1; } if (pool->task_queue) { free(pool->task_queue); } if (pool->threads) { free(pool->threads); pthread_mutex_lock(&(pool->lock)); pthread_mutex_destroy(&(pool->lock)); pthread_mutex_lock(&(pool->thread_counter)); pthread_mutex_destroy(&(pool->thread_counter)); pthread_cond_destroy(&(pool->queue_not_empty)); pthread_cond_destroy(&(pool->queue_not_full)); } free(pool); pool = NULL; return 0; } int threadpool_all_threadnum(threadpool_t *pool) { int all_threadnum = -1; pthread_mutex_lock(&(pool->lock)); all_threadnum = pool->live_thr_num; pthread_mutex_unlock(&(pool->lock)); return all_threadnum; } int threadpool_busy_threadnum(threadpool_t *pool) { int busy_threadnum = -1; pthread_mutex_lock(&(pool->thread_counter)); busy_threadnum = pool->busy_thr_num; pthread_mutex_unlock(&(pool->thread_counter)); return busy_threadnum; } int is_thread_alive(pthread_t tid) { int kill_rc = pthread_kill(tid, 0); //發0號信號,測試線程是否存活 if (kill_rc == ESRCH) { return false; } return true; } /*測試*/ #if 1 /* 線程池中的線程,模擬處理業務 */ void *process(void *arg) { printf("thread 0x%x working on task %d\n ",(unsigned int)pthread_self(),*(int *)arg); sleep(1); printf("task %d is end\n",*(int *)arg); return NULL; } int main(void) { /*threadpool_t *threadpool_create(int min_thr_num, int max_thr_num, int queue_max_size);*/ threadpool_t *thp = threadpool_create(3,100,100);/*創建線程池,池裏最小3個線程,最大100,隊列最大100*/ printf("pool inited"); //int *num = (int *)malloc(sizeof(int)*20); int num[20], i; for (i = 0; i < 20; i++) { num[i]=i; printf("add task %d\n",i); threadpool_add(thp, process, (void*)&num[i]); /* 向線程池中添加任務 */ } sleep(10); /* 等子線程完成任務 */ threadpool_destroy(thp); return 0; }
線程池代碼