#---------------------------------------------需要的了解一下--------------------------------------------------------------
阿里云产品推广
最高¥2000云产品通用代金券
通过链接购买产品即可获得最高2000元的通用产品代金券。
不管你是学生、希望一个简单云主机试水
你是站长、博主需要购买、搭建个人站点、blog
这里有最直接的优惠:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=nsw2bzvo
另外有9折折扣码,比直接购买节省的不是一点两点
https://img-blog.csdnimg.cn/20190828153737618.png
#---------------------------------------------不需要的就当没看见--------------------------------------------------------
最近在公司搭建服务器的时候,用到线程池,想想每次用到线程池都要好好想一想,很烦,索性就做个记录吧。
关于线程池,主要就是现在的服务器往往对并发的要求很高,传统的处理的办法就是作为服务器每次有客户端向我提出服务请求的时候,就需要创建新的线程去处理,这种处理的方式未尝不可,但是,如果对于一些访问量比较大的服务器来说,这种处理的策略显然是致命的。因为当服务器的访问量在某一时间到达峰值的时候,就可能需要在短时间内创建大量的线程,极有可能会导致服务器崩掉。
所以线程池的解决的思路就是,既然在单一的时间点内创建大量线程对服务器不利,那我就事先创建一定数量的线程,等待请求的到来。
使用线程池的服务器的基本的工作模式是:
1。事先创建一定数量的线程。
2.有请求到来的时候,把请求加入到消息队列,
3.线程互斥对消息队列进行访问,如果有消息就进行处理,如果没有消息就等待,
4.动态的保持处在闲置状态下面的线程的说数量。
下面是一个基本的线程池的实现。
threadpool.h
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include "head.h"
//服务队列
typedef services_t ElemType;
typedef struct q_node {
ElemType data;
struct q_node *next, *prev;
}sQueue, *pQueue;
static pQueue init_queue()
{
pQueue pq;
pq = (pQueue)malloc(sizeof(sQueue));
pq->next = pq->prev = pq;
return pq;
}
//线程池的封装结构体
typedef struct {
int thread_num; //线程个数
pthread_t * thread_id; //线程id数组
pQueue thread_queue; //服务读列
pthread_mutex_t thread_mutex; //互斥量
pthread_cond_t thread_cond; //条件变量
}ThreadPool_t;
int threadpool_init(ThreadPool_t*, int, void *(*p)(void*)); //线程池的初始化
int threadpool_destroy(ThreadPool_t*); //线程池的销毁
int threadpool_add(ThreadPool_t*, ElemType*); //向线程池中添加服务请求
int threadpool_get(ThreadPool_t*, ElemType*); //从线程池中取出服务请求
#endif
threadpool.c
#include "threadpool.h"
//static ThreadPool_t * pool;
int threadpool_init(ThreadPool_t *pool, int num, void * (*pfunc)(void*))
{ //线程池的初始化
//pool = (ThreadPool_t*)malloc(sizeof(ThreadPool_t));
pool->thread_num = num;
pool->thread_id = (pthread_t*)malloc(num*sizeof(pthread_t));
pool->thread_queue = init_queue();
pthread_mutex_init(&pool->thread_mutex, NULL);
pthread_cond_init(&pool->thread_cond, NULL);
int i, ret;
for ( i=0; i<num; i++) {
ret = pthread_create(pool->thread_id+i, NULL, pfunc, NULL);
if ( ret != 0 ) {
fprintf(stderr, "pthread_create():%s\n", strerror(ret));
return -1;
}
}
return 0;
}
int threadpool_destroy(ThreadPool_t *pool)
{ //线程池的销毁
int i;
for ( i=0; i<pool->thread_num; i++) {
pthread_cancel(pool->thread_id[i]);
pthread_join(pool->thread_id[i], NULL);
}
pthread_cond_destroy(&pool->thread_cond);
pthread_mutex_destroy(&pool->thread_mutex);
pQueue pdel;
while ( pool->thread_queue->next != pool->thread_queue ) {
pdel = pool->thread_queue->next;
pdel->next->prev = pdel->prev;
pdel->prev->next = pdel->next;
free(pdel);
}
free(pool->thread_queue);
free(pool->thread_id);
//free(pool);
return 0;
}
int threadpool_add(ThreadPool_t *pool, ElemType *data)
{ //向线程池中添加服务请求
pQueue pnew;
pnew = init_queue();
pnew->data = *data;
pthread_mutex_lock(&pool->thread_mutex);
pnew->next = pool->thread_queue;
pnew->prev = pool->thread_queue->prev;
pool->thread_queue->prev->next = pnew;
pool->thread_queue->prev = pnew;
pthread_mutex_unlock(&pool->thread_mutex);
pthread_cond_signal(&pool->thread_cond);
return 0;
}
int threadpool_get(ThreadPool_t *pool, ElemType *data)
{ //从线程池中取出服务请求
pQueue pdel;
pthread_mutex_lock(&pool->thread_mutex);
while ( pool->thread_queue->next == pool->thread_queue ) {
pthread_cond_wait(&pool->thread_cond, &pool->thread_mutex);
}
pdel = pool->thread_queue->next;
pdel->next->prev = pdel->prev;
pdel->prev->next = pdel->next;
pthread_mutex_unlock(&pool->thread_mutex);
*data = pdel->data;
free(pdel);
return 0;
}