線程池概念及用途
-
概念:實質就是一個裝着線程的的容器,線程池一種多線程的處理方式,處理過程間任務添加到隊列,然後創建線程後自動啓動這些任務。
-
比喻:這有點像去街上借的共享充電寶,收集這些充電寶的盒子就是線程池,充電寶就是線程。每個人去拿共享充電寶,就相當於是充電寶接任務。從這個比喻可以知道,共享充電寶收集盒(線程池)裏面要先有一些充電寶(已經創建好的線程)纔行。
-
爲什麼要有線程池:節省資源,每次來一個客戶端都要創建、銷燬、運行,這會消耗大量資源(因爲創建和銷燬會佔用時間)。如果提前創建好一堆線程,那麼就節省了創建和銷燬的是時間。
-
線程池用途:在服務器有客戶端接入的時候就會創建線程(來一個客戶端創一個),而線程池就是管理這些線程的。
模型機制
任務隊列的任務會交給線程池的線程來處理。這當中主要運用到的是信號量和條件變量來實現線程的阻塞和接任務。下面我分成了三部分:服務器(任務隊列)、客戶端、線程池。
當任務隊列不爲空,且沒有滿的時候:
- 在服務器(任務隊列)和任務池兩個之間,服務器充當生產者,任務池充當消費者。
- 在客戶端和服務器(任務隊列)間,客戶端充當生產者,服務器(任務隊列)充當消費者。
當任務隊列的任務超出以建好線程數量時:再創建出多個線程。
示例代碼
下面是代碼書寫時候的主要流程,和具體的操作
main文件
主要作用:創建線程池,接受客戶端的申請,每個客戶端接入就往任務列表中加入一個任務。
#include "threadpool.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
//每個客戶端創建出來的任務
void* mytask(void *arg)
{
printf("thread 0x%x is working on task %d\n", (int)pthread_self(), *(int*)arg);
sleep(1);
free(arg);
return NULL;
}
int main(void)
{
//建立一個線程池
threadpool_t *pool;
threadpool_init(&pool, 10); //線程上限最多10個
//這裏使用i來模擬客戶端的請求接入
int i;
for (i=0; i<10; i++)
{
int *arg = (int *)malloc(sizeof(int));
*arg = i;
//給任務隊列添加任務,該任務爲mytask(在main函數上面),arg是傳到mytask中的參數
threadpool_add_task(pool, mytask, arg);
}
//sleep(15);
threadpool_destroy(pool);
return 0;
}
condition.h和condition.c文件
作用:這兩個文件只要是提供條件變量和互斥鎖的使用方法。
condition.h文件
#ifndef _CONDITION_H_
#define _CONDITION_H_
#include <pthread.h>
//定義一個專門儲藏互斥鎖和條件變量的結構體
typedef struct condition
{
pthread_mutex_t pmutex;
pthread_cond_t pcond;
} condition_t;
//結構體初始化
int condition_init(condition_t *cond);
//互斥鎖上所
int condition_lock(condition_t *cond);
//互斥鎖解鎖
int condition_unlock(condition_t *cond);
//條件變量等待
int condition_wait(condition_t *cond);
//條件變量輪詢等待
int condition_timedwait(condition_t *cond, const struct timespec *abstime);
//條件變量喚醒
int condition_signal(condition_t *cond);
//條件變量廣播(全部喚醒)
int condition_broadcast(condition_t *cond);
//條件變量和互斥量銷燬
int condition_destroy(condition_t *cond);
#endif /* _CONDITION_H_ */
condition.c文件
#include "condition.h"
//互斥鎖、條件變量初始化
int condition_init(condition_t *cond)
{
int status;
if ((status = pthread_mutex_init(&cond->pmutex, NULL)))
return status;
if ((status = pthread_cond_init(&cond->pcond, NULL)))
return status;
return 0;
}
//互斥鎖加鎖
int condition_lock(condition_t *cond)
{
return pthread_mutex_lock(&cond->pmutex);
}
//互斥鎖解鎖
int condition_unlock(condition_t *cond)
{
return pthread_mutex_unlock(&cond->pmutex);
}
//條件變量等待
int condition_wait(condition_t *cond)
{
return pthread_cond_wait(&cond->pcond, &cond->pmutex);
}
//條件變量輪詢等待
int condition_timedwait(condition_t *cond, const struct timespec *abstime)
{
return pthread_cond_timedwait(&cond->pcond, &cond->pmutex, abstime);
}
//條件變量喚醒
int condition_signal(condition_t *cond)
{
return pthread_cond_signal(&cond->pcond);
}
//條件變量和互斥鎖銷燬
int condition_destroy(condition_t* cond)
{
int status;
if ((status = pthread_mutex_destroy(&cond->pmutex)))
return status;
if ((status = pthread_cond_destroy(&cond->pcond)))
return status;
return 0;
}
threadpool.c和threadpool.h文件
作用:實現線程池裏面的線程和任務隊列的連接工作,還有一些於線程初始化、銷燬相關的函數。
threadpool.h文件
#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_
#include "condition.h"
// 任務結構體,將任務放入隊列由線程池中的線程來執行
typedef struct task
{
void *(*run)(void *arg); // 任務回調函數
void *arg; // 回調函數參數
struct task *next; // 鏈表隊列
} task_t;
// 線程池結構體
typedef struct threadpool
{
condition_t ready; //任務準備就緒或者線程池銷燬通知,這個結構體在條件變量類中
task_t *first; //任務隊列頭指針
task_t *last; //任務隊列尾指針
int counter; //線程池中當前線程數
int idle; //線程池中當前正在等待任務的線程數
int max_threads; //線程池中最大允許的線程數
int quit; //銷燬線程池的時候置1
} threadpool_t;
// 初始化線程池
void threadpool_init(threadpool_t **pool, int threads);
// 往線程池中添加任務
void threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg);
// 銷燬線程池
void threadpool_destroy(threadpool_t *pool);
#endif /* _THREAD_POOL_H_ */
threadpool.c文件
#include "threadpool.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
void *thread_routine(void *arg)
{
struct timespec abstime;
threadpool_t *pool = (threadpool_t *)arg;
printf("thread 0x%x is starting\n", (int)pthread_self());
//1. 設置爲自分離線程
pthread_detach(pthread_self());
//3. 進入輪詢工作模式,如果不退出且隊列不爲空則一直工作
while(1)
{
//1. 先去看下有沒有任務,有任務則處理任務,沒有再wait
condition_lock(&pool->ready);
printf("thread 0x%x is working\n", (int)pthread_self());
if(pool->first != NULL) //這代表任務隊列有任務
{
//把隊列的第一個任務拿走,然後原本的第二個任務變成第一個任務
task_t *t = pool->first; //取出第一個任務
pool->first = t->next; //修改隊列頭
condition_unlock(&pool->ready); //先解鎖,提高效率
//處理任務
t->run(t->arg);
free(t);
continue; //既然本次有任務,可能下次還有任務,則繼續查看是否有任務
}
else
{
//沒有任務,把互斥鎖解鎖,繼續等待
condition_unlock(&pool->ready);
}
if(pool->quit)
{
break;
}
//2. 如果沒有任務,則等待
printf("thread 0x%x is waiting\n", (int)pthread_self());
while(1)
{
//設置輪詢時間
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += 2; //延時2s
//condition_wait(&pool->ready); //使用條件變量的等待也可以
condition_lock(&pool->ready);
//線程沒有接到任務,線程池中正在等待的線程數+1
pool->idle++;
//輪詢條件變量(輪詢任務隊列裏面有沒有新的任務)
int status = condition_timedwait(&pool->ready, &abstime);
condition_unlock(&pool->ready);
if (status != ETIMEDOUT || pool->quit)
{
printf("thread 0x%x 線程被喚醒了\n", (int)pthread_self());
break; //注意:跳出當前循環,進入外面的while中,而不是跳到THREAD_EXIT
}
else
{
printf("thread 0x%x 等待時間超過了\n", (int)pthread_self());
//限定當前線程數爲3個,超過的關閉。因爲沒那麼多的任務,線程可以不用那麼多了
if(pool->counter >= 3)
{
goto THREAD_EXIT;
}
}
}//最裏層while結束
}//最外層while結束
THREAD_EXIT:
printf("thread 0x%x 退出\n", (int)pthread_self());
condition_lock(&pool->ready);
pool->counter--;
condition_unlock(&pool->ready);
pthread_exit(NULL); //退出線程
}
//初始化線程池結構體
void threadpool_init(threadpool_t **pool, int threads)
{
//1. 初始化基本的線程池參數
int i;
threadpool_t *newpool = malloc(sizeof(threadpool_t));
*pool = newpool;
newpool->max_threads = threads; //最大的線程數不能
newpool->quit = 0; //銷燬線程池的時候置1
newpool->idle = 0; //線程池中當前正在等待任務的線程數
newpool->first = NULL; //任務隊列頭指針
newpool->last = NULL; //任務隊列尾指針
newpool->counter = 0; //線程池中當前線程數
condition_init(&newpool->ready);//條件變量類裏面的函數,創建一個條件變量
//2. 默認有線程數,則在初始化的時候同時初始化N個線程
#if 1
for(i= 0; i < threads; i++)
{
pthread_t tid;
if(pthread_create(&tid, NULL, thread_routine, newpool) == 0)//where is task?
{
condition_lock(&newpool->ready);
newpool->counter++;
condition_unlock(&newpool->ready);
}
}
#endif
}
void threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg)
{
if(pool->quit)
return;
//1. 生成任務包
task_t *task = malloc(sizeof(task_t));
task->run = run;
task->arg = arg;
//2. 加到task隊列, 先上鎖,再添加,再解鎖
printf("Add new task %p ! \n", task);
condition_lock(&pool->ready);
if(pool->last == NULL) //if這裏是在隊列一次進來的時候執行的(隊列裏什麼都沒有的時候)
{
pool->last = task; //隊列頭
pool->first = pool->last; //初始化頭
}
else
{
pool->last->next = task; // add
pool->last = task;
}
//3. 計算一下線程數是否滿足任務處理速度,不滿足則創建一批
if(pool->counter < pool->max_threads && pool->idle <= 0) //當前線程數<最大線程數,且空閒線<=0
{
//??線程創建策略,根據實際環境選擇
// 策略1: 固定增長,每次增長??
// 策略2: 指數增長,每次翻倍?? 也就是創建 pool->counter
// 策略3: 線下增長,每次+1
// 策略4: 根據任務數量增長
pthread_t tid;
if(pthread_create(&tid, NULL, thread_routine, pool) == 0) //創建出來的線程,有任務就去接任務,沒任務就輪詢任務列表,等待被喚醒去接任務
{
pool->counter++;
}
}
//4. 通知線程去取任務處理
if(pool->idle > 0)
{
condition_signal(&pool->ready); //喚醒一個線程去處理任務
}
//5. 解鎖
condition_unlock(&pool->ready);
}
void threadpool_destroy(threadpool_t *pool)
{
//1. 設置退出條件
pool->quit = 1;
//2. 等待所有線程退出
while(pool->counter > 0)
{
//3. 廣播,通知所有線程退出
condition_lock(&pool->ready);
condition_broadcast(&pool->ready); //喚醒所有線程退出
condition_unlock(&pool->ready);
sleep(1);
}
//4. 銷燬線程池對象
free(pool);
}