科百科
当前位置: 首页 范文大全

c语言什么时候需要线程池(线程池原理详解及如何用C语言实现线程池)

时间:2023-08-10 作者: 小编 阅读量: 3 栏目名: 范文大全

任务队列信息:描述当前任务队列基本信息,如最大任务数、队列不为满条件变量、队列不为空条件变量等……

线程池是一种多线程处理形式,大多用于高并发服务器上,它能合理有效的利用高并发服务器上的线程资源;线程与进程用于处理各项分支子功能,我们通常的操作是:接收消息 ==> 消息分类 ==> 线程创建 ==> 传递消息到子线程 ==> 线程分离 ==> 在子线程中执行任务 ==> 任务结束退出;

对大多数小型局域网的通信来说,上述方法足够满足需求;但当我们的通信范围扩大到广域网或大型局域网通信中时,我们将面临大量消息频繁请求服务器;在这种情况下,创建与销毁线程都已经成为一种奢侈的开销,特别对于嵌入式服务器来说更应保证内存资源的合理利用;

因此,线程池技术应运而生;线程池允许一个线程可以多次复用,且每次复用的线程内部的消息处理可以不相同,将创建与销毁的开销省去而不必来一个请求开一个线程;

结构讲解:

线程池是一个抽象的概念,其内部由任务队列,一堆线程,管理者线程组成;

我们将以上图为例,实现一个最基础的线程池,接下来将分部分依次讲解;讲解顺序为:1.线程池总体结构 2.线程数组 3.任务队列 4.管理者线程 5.使用线程池接口的例子

一、线程池总体结构

这里讲解线程池在逻辑上的结构体;看下方代码,该结构体threadpool_t中包含线程池状态信息,任务队列信息以及多线程操作中的互斥锁;在任务结构体中包含了一个可以放置多种不同任务函数的函数指针,一个传入该任务函数的void*类型的参数;

注意:在使用时需要将你的消息分类处理函数装入任务的(*function);然后放置到任务队列并通知空闲线程;

线程池状态信息:描述当前线程池的基本信息,如是否开启、最小线程数、最大线程数、存活线程数、忙线程数、待销毁线程数等… …

任务队列信息:描述当前任务队列基本信息,如最大任务数、队列不为满条件变量、队列不为空条件变量等… …

多线程互斥锁:保证在同一时间点上只有一个线程在任务队列中取任务并修改任务队列信息、修改线程池信息;

函数指针:在打包消息阶段,将分类后的消息处理函数放在(*function);

void*类型参数:用于传递消息处理函数需要的信息;

/*任务*/

typedef struct {

void *(*function)(void *);

void *arg;

} threadpool_task_t;

/*线程池管理*/

struct threadpool_t{

pthread_mutex_t lock; /* 锁住整个结构体 */

pthread_mutex_t thread_counter; /* 用于使用忙线程数时的锁 */

pthread_cond_t queue_not_full; /* 条件变量,任务队列不为满 */

pthread_cond_t queue_not_empty; /* 任务队列不为空 */

pthread_t *threads; /* 存放线程的tid,实际上就是管理了线 数组 */

pthread_t admin_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; /* 队头 */

int queue_rear; /* 队尾 */

int queue_size;

/* 存在的任务数 */

int queue_max_size; /* 队列能容纳的最大任务数 */

/*线程池状态*/

int shutdown; /* true为关闭 */

};

**/*创建线程池*/**

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 false; \n");

break;

}

/*信息初始化*/

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->wait_exit_thr_num = 0;

pool->queue_front = 0;

pool->queue_rear = 0;

pool->queue_size = 0;

pool->queue_max_size = queue_max_size;

pool->shutdown = false;

/* 根据最大线程数,给工作线程数组开空间,清0 */

pool->threads = (pthread_t *)malloc(sizeof(pthread_t)*max_thr_num);

if (pool->threads == NULL)

{

printf("malloc threads false;\n");

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 false;\n");

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 lock or cond false;\n");

break;

}

/* 启动min_thr_num个工作线程 */

for (i=0; i<min_thr_num; i)

{

/* pool指向当前线程池 threadpool_thread函数在后面讲解 */

pthread_create(&(pool->threads[i]), NULL, threadpool_thread, (void *)pool);

printf("start thread 0x%x... \n", (unsigned int)pool->threads[i]);

}

/* 管理者线程 admin_thread函数在后面讲解 */

pthread_create(&(pool->admin_tid), NULL, admin_thread, (void *)pool);

return pool;

} while(0);

/* 释放pool的空间 */

threadpool_free(pool);

return NULL;

}

二、线程数组

线程数组实际上是在线程池初始化时开辟的一段存放一堆线程tid的空间,在逻辑上形成一个池,里面放置着提前创建的线程;这段空间中包含了正在工作的线程,等待工作的线程(空闲线程),等待被销毁的线程,申明但没有初始化的线程空间;

/*工作线程*/

void *

threadpool_thread(void *threadpool)

{

threadpool_t *pool = (threadpool_t *)threadpool;

threadpool_task_t task;

while (true)

{

pthread_mutex_lock(&(pool->lock));

/* 无任务则阻塞在 “任务队列不为空” 上,有任务则跳出 */

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));

/* 判断是否需要清除线程,自杀功能 */

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);//结束线程

}

}

}

/* 线程池开关状态 */

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_front1) % 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;

pthread_mutex_unlock(&(pool->thread_counter));

(*(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--;

pthread_mutex_unlock(&(pool->thread_counter));

}

pthread_exit(NULL);

}

三、任务队列

任务队列的存在形式与线程数组相似;在线程池初始化时根据传入的最大任务数开辟空间;当服务器前方后请求到来后,分类并打包消息成为任务,将任务放入任务队列并通知空闲线程来取;不同之处在于任务队列有明显的先后顺序,先进先出;而线程数组中的线程则是一个竞争关系去拿到互斥锁争取任务;


/*向线程池的任务队列中添加一个任务*/

int

threadpool_add_task(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));

return -1;

}

/*清空工作线程的回调函数的参数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_rear1) % pool->queue_max_size; /* 逻辑环 */

pool->queue_size;

/*添加完任务后,队列就不为空了,唤醒线程池中的一个线程*/

pthread_cond_signal(&(pool->queue_not_empty));

pthread_mutex_unlock(&(pool->lock));

return 0;

}

四、管理者线程

作为线程池的管理者,该线程的主要功能包括:检查线程池内线程的存活状态,工作状态;负责根据服务器当前的请求状态去动态的增加或删除线程,保证线程池中的线程数量维持在一个合理高效的平衡上;

说到底,它就是一个单独的线程,定时的去检查,根据我们的一个维持平衡算法去增删线程;

/*管理线程*/

void *

admin_thread(void *threadpool)

{

int i;

threadpool_t *pool = (threadpool_t *)threadpool;

while (!pool->shutdown)

{

printf("admin -----------------\n");

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));

printf("admin busy live -%d--%d-\n", busy_thr_num, live_thr_num);

/*创建新线程 实际任务数量大于 最小正在等待的任务数量,存活线程数小于最大线程数*/

if (queue_size >= MIN_WAIT_TASK_NUM && live_thr_num <= pool->max_thr_num)

{

printf("admin add-----------\n");

pthread_mutex_lock(&(pool->lock));

int add=0;

/*一次增加 DEFAULT_THREAD_NUM 个线程*/

for (i=0; i<pool->max_thr_num && add<DEFAULT_THREAD_NUM

&& 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;

printf("new thread -----------------------\n");

}

}

pthread_mutex_unlock(&(pool->lock));

}

/*销毁多余的线程 忙线程x2 都小于 存活线程,并且存活的大于最小线程数*/

if ((busy_thr_num*2) < live_thr_num && live_thr_num > pool->min_thr_num)

{

// printf("admin busy --%d--%d----\n", busy_thr_num, live_thr_num);

/*一次销毁DEFAULT_THREAD_NUM个线程*/

pthread_mutex_lock(&(pool->lock));

pool->wait_exit_thr_num = DEFAULT_THREAD_NUM;

pthread_mutex_unlock(&(pool->lock));

for (i=0; i<DEFAULT_THREAD_NUM; i)

{

//通知正在处于空闲的线程,自杀

pthread_cond_signal(&(pool->queue_not_empty));

printf("admin cler --\n");

}

}

}

return NULL;

/*线程是否存活*/

int

is_thread_alive(pthread_t tid)

{

int kill_rc = pthread_kill(tid, 0); //发送0号信号,测试是否存活

if (kill_rc == ESRCH) //线程不存在

{

return false;

}

return true;

}

五、释放

/*释放线程池*/

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_destroy(threadpool_t *pool)

{

int i;

if (pool == NULL)

{

return -1;

}

pool->shutdown = true;

/*销毁管理者线程*/

pthread_join(pool->admin_tid, NULL);

//通知所有线程去自杀(在自己领任务的过程中)

for (i=0; i<pool->live_thr_num; i)

{

pthread_cond_broadcast(&(pool->queue_not_empty));

}

/*等待线程结束 先是pthread_exit 然后等待其结束*/

for (i=0; i<pool->live_thr_num; i)

{

pthread_join(pool->threads[i], NULL);

}

threadpool_free(pool);

return 0;

}

六、接口

/* 线程池初始化,其管理者线程及工作线程都会启动 */

threadpool_t *thp = threadpool_create(10, 100, 100);

printf("threadpool init ... ... \n");

/* 接收到任务后添加 */

threadpool_add_task(thp, do_work, (void *)p);

// ... ...

/* 销毁 */

threadpool_destroy(thp);

需要C/CLinux服务器开发学习资料私信“资料”(资料包括C/C,Linux,golang技术,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK,ffmpeg等),免费分享

    推荐阅读
  • 散尾葵开花有啥寓意(散尾葵开花有什么兆头)

    飞黄腾达散尾葵开花还有飞黄腾达的兆头,它的花朵很小,数量繁多,花朵的颜色为金黄色,花期时间在每年的5月份左右,我们可以将其送给同事或者朋友,祝愿对方事业一帆风顺,希望它能够早日飞黄腾达。主要价值散尾葵的观赏价值很高,它是一种小型的棕榈植物,耐阴能力很强,我们可以将其养护在家中来美化环境,它还能净化空气,有益于人体的身心健康,还可以将其作为绿化植物栽种在草地、树荫等地方。

  • 成熟沧桑的网名(成熟沧桑的网名精选)

    以下内容希望对你有帮助!成熟沧桑的网名旧梦不再光辉岁月回忆在沉淀曾经沧海一世沧桑岁月催人老人生几何半程客梦看透ㄋ世事悲凉旧风年间我怀念的往事堪嗟恍如隔世尘埃落定微笑面对深藏功与名繁华落尽终成殇

  • 怎么做盐津桃肉(做盐津桃肉的方法)

    下面更多详细答案一起来看看吧!怎么做盐津桃肉去皮后臀尖猪肉250克,青蒜苗100克,郫县豆瓣1汤匙,豆豉2茶匙,盐3克,糖5ml,油30ml。将猪肉切成大薄片,答青蒜苗洗净斜切成段,郫县豆瓣和豆豉剁碎。锅烧热放入油,放入肉片和盐翻炒,炒到肉片稍变色出油。加入郫县豆瓣和豆豉炒匀,再加入蒜苗炒匀,加入糖调味即可。

  • 华为的激励性考核机制(考核与评价的高阶逻辑)

    《华为基本法》中“考核与评价”的文本非常精练,只有第六十五、六十六两条,然而内容却具有特别的高度。中国传统思想则与管理学的基本假设不一致。考核与评价是华为整个人力资源管理的枢纽。尽管Y理论的创始人麦格雷戈认为Y理论假设下企业不需要考核与评价,华为却认为这仍然是必要的。当然,考核与评价并不是华为创造的,这套体系在西方企业已被广泛应用。

  • 保定市婚纱摄影店推荐(保定婚纱摄影排名前十)

    第1名:保定苏菲雅婚纱摄影网友评论:我在有榜网上预约的他家,并且领取了礼物和优惠,实际便宜了不少。第2名:保定新新娘婚纱摄影网友评论:片子已经出来了,就等过段时间取片了。第16名:保定绝色婚纱摄影网友评论:摄影技术,整体环境非常不错!我只能说各有各的眼光,各有各的爱好第19名:保定华纳婚纱摄影网友评论:摄影师、化妆师、服装师都是非常棒的!

  • 阿意顺旨是什么意思(阿意顺旨指什么)

    接下来我们就一起去研究一下吧!阿意顺旨是什么意思阿意顺旨,汉语成语,拼音ēyìshùnzhǐ,意思是曲意逢迎,顺从君主的意图。出自《野客丛书·汉人规戒》:“汉人于交友故旧,动存规戒,其不肯阿意顺旨,以限于非义,此风凛然可喜。”

  • 温州退役军人和三属持优待证可免费享受公共交通、文旅等优待

    城市公共交通2023年1月10日起温州市户籍的退役军人持优待证在各轨道交通站点人工服务台换取车票,免费乘坐温州轨道交通S1线。市外退役军人和“三属”持优待证到温州市退役军人服务中心申请乘车码,申请后扫乘车码免费乘坐温州市区公共交通。特别说明:65周岁以上的退役军人、残疾军人和“三属”对象凭原有证件享受免费公交等优待,现在仍然有效,可自行选择使用何种证件。温州市国有A级旅游景区(景点)免费优待名单

  • 小米盒子怎么重置(小米技巧)

    把小米盒子断电同时按下<主页>+<菜单>两个按键不要松开,遥控器对着盒子并给盒子加电启动,下面我们就来说一说关于小米盒子怎么重置?我们一起去了解并探讨一下这个问题吧!等待系统删除数据,并重新出现选择界面。重启过后就可以进入小米盒子的正常界面了,这样就可以再次正常使用小米盒子了。

  • 梦见家人出车祸(梦见家人出车祸什么意思)

    女人梦见家人出车祸了,因注意力散漫闯了不少小祸,尤其对受伤、事故等更要留意了。另外还有对你的做法挑剔找麻烦的人出现,做事较容易受阻碍。健康运也较差,特别对食物的卫生方面需多留意。孕妇梦见家人出车祸了,容易半途而废或一下热情退烧就对某件事厌倦了。这样的游乐态度让周围的人感到有些头痛喔。此外,先想好这两天可能会前往的地方或见到的人,穿上合宜的服装出门就是这两天的重点啦。