知识点:UNIX网络编程30章 ,UNIX网络编程26章
书上30.12有一个预先创建线程服务器程序的例子,虽然比较简单,但是流程很清晰。
Thread可看作是一个线程结构,不过书上只有两个变量,等下后面一个复杂点的程序在线程结构上加了很多的内容:
typedef struct tp_thread_info_s {
pthread_t thread_id; //thread id num
bool is_busy; //thread status:true-busy;flase-idle
pthread_cond_t thread_cond;
pthread_mutex_t thread_lock;
process_job proc_fun; //线程执行函数
TpWorkDesc* th_job; //函数参数,结构的形式
TpThreadPool* tp_pool;
}TpThreadInfo;
在这里,结构里面加了线程执行函数,以及函数参数,创建线程时把一个结构传进去,这比书上传入一个参数来得高级得多,这里不同线程修改proc_fun可以实现线程功能的多样化。
书上的clifd数组,iget,iput是线程池中的变量,MAXNCLI相当于下面的cur_th_num, clifd相当于TpThreadInfo指针数组,iput,iget就相当于下面vector实现的功能:
struct tp_thread_pool_s {
unsigned min_th_num; //min thread number in the pool
unsigned cur_th_num; //current thread number in the pool
unsigned max_th_num; //max thread number in the pool
pthread_mutex_t tp_lock;
pthread_t manage_thread_id; //manage thread id num
TpThreadInfo* thread_info;
vector<TpThreadInfo*>idle_q;
bool stop_flag;
} ;
对线程池及其中的线程初始化之后,书上线程是调用thread_make函数,我这里是tp_work_thread函数,等待信号到来执行任务。在这里可以看出传入一个结构的好处了,可以执行结构中的函数体,而不是像书上所有线程执行相同的函数体。
这里比较关注是线程调度函数,书上的利用一个数组,依次调用,到数组结尾了又返回前面。这里有个出错判断,当添加任务的速度快于处理任务的速度时,iput有可能赶上iget,要解决这个问题,可以把数组刚开始设置很大,或者在这里扩大数组。 另外,线程执行完了没有标志判断,取走任务了,可能线程还在处理中,又添加一个任务发送信号给这个线程,所以 可以看出,这个线程调度函数有很大的缺陷。我这里是 tp_process_job函数,我用了一个vector来实现,vector本质上也是数组,我这里是取或者存入都是在尾部进行,所以vector也不会有很大的负担。
bool tp_process_job(TpThreadPool *pTp, process_job proc_fun, TpWorkDesc *job) { //xianchengdiaodu
TpThreadInfo *pThi ;
//fill pTp->thread_info's relative work key
pthread_mutex_lock(&pTp->tp_lock);
pThi = pTp->idle_q.back();
pTp->idle_q.pop_back();
pthread_mutex_unlock(&pTp->tp_lock);
if(pThi){
pThi->is_busy =TRUE;
pThi->proc_fun = proc_fun;
pThi->th_job = job;
pthread_cond_signal(&pThi->thread_cond);
printf("%d Fetch a thread from pool.\n", pThi-pTp->thread_info);
return TRUE;
}
else
printf("the thread is pool\n");
}
暂时理解这么多,后面有时间再完善一下。网上的代码原理讲得清楚了,但是不能执行,有很多错误,我把别人的代码修改了一下,终于可以执行了,可以设计自己的api了,有些部分需要再琢磨一下,下面这段代码已经调试好了,可以执行。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<vector>
#include<string.h>
#include<signal.h>
#include<error.h>
#include<iostream>
using namespace std;
#define TRUE 1
#define FALSE 0
typedef struct tp_work_desc_s TpWorkDesc;
typedef void (*process_job)(TpWorkDesc*job);
typedef struct tp_thread_pool_s TpThreadPool;
static void *tp_work_thread(void *arg);
struct tp_work_desc_s {
void *ret; //call in, that is arguments
void *arg; //call out, that is return value
};
typedef struct tp_thread_info_s {
pthread_t thread_id; //thread id num
bool is_busy; //thread status:true-busy;flase-idle
pthread_cond_t thread_cond;
pthread_mutex_t thread_lock;
process_job proc_fun;
TpWorkDesc* th_job;
TpThreadPool* tp_pool;
}TpThreadInfo;
struct tp_thread_pool_s {
unsigned min_th_num; //min thread number in the pool
unsigned cur_th_num; //current thread number in the pool
unsigned max_th_num; //max thread number in the pool
pthread_mutex_t tp_lock;
pthread_t manage_thread_id; //manage thread id num
TpThreadInfo* thread_info;
vector<TpThreadInfo*>idle_q;
bool stop_flag;
} ;
TpThreadPool *tp_create(unsigned min_num, unsigned max_num) {
TpThreadPool* pTp;
pTp = (TpThreadPool *)malloc(sizeof(TpThreadPool)*max_num);
memset(pTp, 0, sizeof(TpThreadPool));
pTp->min_th_num = min_num;
pTp->cur_th_num = min_num;
pTp->max_th_num = max_num;
pTp->tp_lock = PTHREAD_MUTEX_INITIALIZER;
//pTp->thread_cond = PTHREAD_COND_INITIALIZER;
pTp->thread_info = (TpThreadInfo *)malloc(sizeof(TpThreadInfo)*max_num);
memset(pTp->thread_info, 0, sizeof(TpThreadInfo)*max_num);
pTp->stop_flag = FALSE;
//xianshiduilie
return pTp;
}
bool tp_init(TpThreadPool *pTp){
TpThreadInfo* pThi;
for(int i=0;i<pTp->min_th_num;i++)
{
pThi = pTp->thread_info+i;
pTp->idle_q.push_back(pThi); //
pThi->is_busy = FALSE;
pThi->thread_cond = PTHREAD_COND_INITIALIZER;
pThi->thread_lock = PTHREAD_MUTEX_INITIALIZER;
pThi->proc_fun = NULL;
pThi->th_job = NULL;
pThi->tp_pool = pTp;
int err = pthread_create(&pThi->thread_id, NULL, tp_work_thread, (void *)pThi);
if (0 != err) {
printf("tp_init: create work thread failed.");
pTp->idle_q.clear();
return FALSE;
}
}
/*int err = pthread_create(&pTp->manage_thread_id, NULL, tp_manage_thread, pTp);
if (0 != err) {
pTp->idle_q.clear();
printf("tp_init: creat manage thread failed\n");
return FALSE;
} */
return TRUE;
}
void tp_close(TpThreadPool *pTp, bool wait) {
unsigned i;
pTp->stop_flag = TRUE;
if (wait) {
for (i = 0; i < pTp->cur_th_num; i++) {
pthread_cond_signal(&pTp->thread_info[i].thread_cond);
}
for (i = 0; i < pTp->cur_th_num; i++) {
pthread_join(pTp->thread_info[i].thread_id, NULL);
pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);
pthread_cond_destroy(&pTp->thread_info[i].thread_cond);
}
} else {
//close work thread
for (i = 0; i < pTp->cur_th_num; i++) {
kill((pid_t)pTp->thread_info[i].thread_id, SIGKILL);
pthread_mutex_destroy(&pTp->thread_info[i].thread_lock);
pthread_cond_destroy(&pTp->thread_info[i].thread_cond);
}
}
//close manage thread
kill((pid_t)pTp->manage_thread_id, SIGKILL);
pthread_mutex_destroy(&pTp->tp_lock);
//free thread struct
free(pTp->thread_info);
pTp->thread_info = NULL;
}
bool tp_process_job(TpThreadPool *pTp, process_job proc_fun, TpWorkDesc *job) { //xianchengdiaodu
TpThreadInfo *pThi ;
//fill pTp->thread_info's relative work key
pthread_mutex_lock(&pTp->tp_lock);
pThi = pTp->idle_q.back();
pTp->idle_q.pop_back();
pthread_mutex_unlock(&pTp->tp_lock);
if(pThi){
pThi->is_busy =TRUE;
pThi->proc_fun = proc_fun;
pThi->th_job = job;
pthread_cond_signal(&pThi->thread_cond);
printf("%d Fetch a thread from pool.\n", pThi-pTp->thread_info);
return TRUE;
}
else
printf("the thread is pool\n");
}
static void *tp_work_thread(void *arg) {
pthread_t curid;//current thread id
TpThreadInfo *pTinfo = (TpThreadInfo *) arg;
//wait cond for processing real job.
while (!(pTinfo->tp_pool->stop_flag)) {
pthread_mutex_lock(&pTinfo->thread_lock);
pthread_cond_wait(&pTinfo->thread_cond, &pTinfo->thread_lock);
pthread_mutex_unlock(&pTinfo->thread_lock);
//process
pTinfo->proc_fun(pTinfo->th_job);
//thread state be set idle after work
//pthread_mutex_lock(&pTinfo->thread_lock);
pTinfo->is_busy = FALSE;
pTinfo->tp_pool->idle_q.push_back(pTinfo);
//pthread_mutex_unlock(&pTinfo->thread_lock);
printf("Job done, %d am idle now.\n",pTinfo-pTinfo->tp_pool->thread_info );
}
}
/*
TpThreadInfo *tp_add_thread(TpThreadPool *pTp) {
int err;
TpThreadInfo *new_thread;
if (pTp->max_th_num <= pTp->cur_th_num)
return NULL;
//malloc new thread info struct
new_thread = pTp->thread_info + pTp->cur_th_num;
new_thread->tp_pool = pTp;
//init new thread's cond & mutex
pthread_cond_init(&new_thread->thread_cond, NULL);
pthread_mutex_init(&new_thread->thread_lock, NULL);
//init status is busy, only new process job will call this function
new_thread->is_busy = TRUE;
err = pthread_create(&new_thread->thread_id, NULL, tp_work_thread, new_thread);
if (0 != err) {
free(new_thread);
return NULL;
}
//add current thread number in the pool.
pTp->cur_th_num++;
return new_thread;
}
int tp_get_tp_status(TpThreadPool *pTp) {
float busy_num = 0.0;
int i;
//get busy thread number
busy_num = pTp->cur_th_num - pTp->idle_q.count;
DEBUG("Current thread pool status, current num: %u, busy num: %u, idle num: %u\n", pTp->cur_th_num, (unsigned)busy_num, pTp->idle_q.count);
//0.2? or other num?
if (busy_num / (pTp->cur_th_num) < BUSY_THRESHOLD)
return 0;//idle status
else
return 1;//busy or normal status
}
bool tp_delete_thread(TpThreadPool *pTp) {
unsigned idx;
TpThreadInfo *pThi;
TpThreadInfo tT;
//current thread num can't < min thread num
if (pTp->cur_th_num <= pTp->min_th_num)
return FALSE;
//pthread_mutex_lock(&pTp->tp_lock);
pThi = deQueue(&pTp->idle_q);
//pthread_mutex_unlock(&pTp->tp_lock);
if(!pThi)
return FALSE;
//after deleting idle thread, current thread num -1
pTp->cur_th_num--;
memcpy(&tT, pThi, sizeof(TpThreadInfo));
memcpy(pThi, pTp->thread_info + pTp->cur_th_num, sizeof(TpThreadInfo));
//kill the idle thread and free info struct
kill((pid_t)tT.thread_id, SIGKILL);
pthread_mutex_destroy(&tT.thread_lock);
pthread_cond_destroy(&tT.thread_cond);
return TRUE;
}
*/
#define THD_NUM 10
void proc_fun(TpWorkDesc *job){
int i;
int idx=*(int *)job->arg;
printf("Begin: thread %d\n", idx);
sleep(3);
printf("End: thread %d\n", idx);
}
int main(int argc, char **argv){
TpThreadPool *pTp= tp_create(15,20);
TpWorkDesc pWd[THD_NUM];
int i, *idx;
tp_init(pTp);
for(i=0; i < THD_NUM; i++){
idx=(int *) malloc(sizeof(int));
*idx=i;
pWd[i].arg=idx;
tp_process_job(pTp, proc_fun, pWd+i);
usleep(400000);
}
sleep(1);
tp_close(pTp, TRUE);
free(pTp);
printf("All jobs done!\n");
exit(0);
}
原文地址:http://blog.chinaunix.net/uid-26983585-id-3336491.html