网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
需要考虑的技术问题一,线程池应该包含哪些成员变量。
既然要开一定数量的线程,那么这个“一定数量(max_thread_num)”必定是线程池的一个成员。
如何表示一个线程池是否已经关闭?如果关闭那么必需要立马释放资源。所以“是否关闭(shutdown)”也是一个成员。
创建线程需要有id,必需要为每个线程准备一个id,所以需要一个id数组,其长度就是max_thread_num。
线程锁,用以保证对线程操作时的互斥性。所以需要一个锁,queue_lock。
条件变量(condition_variable),这里使用条件变量主要是为了广播任务到来的消息给所有线程。当有处于空闲的线程,则由此线程
接受任务分派。所以需要一个条件变量queue_ready。
最为重要的就是任务本身,也就是工作。那么工作本身又需要哪几个成员变量?首先肯定是任务入口,routine函数;
其次是routine函数的参数args;再次任务是以队列存在着的,所以任务本身应该包含一个next。
需要考虑的技术问题二,线程池应该包含哪些api。
一、创建线程池,create_tpool
二、销毁线程池,destroy_tpool
三、分派任务,add_task_2_tpool
基于上述分析,我们可以先构造头文件。
tpool.h
#ifndef T\_POOL
#define T\_POOL
#include <pthread.h>
#include <ctype.h>
typedef struct tpool\_work{
void\* (\*work_routine)(void\*); //function to be called
void\* args; //arguments
struct tool\_work\* next;
}tpool\_work\_t;
typedef struct tpool{
size\_t shutdown; //is tpool shutdown or not, 1 ---> yes; 0 ---> no
size\_t maxnum_thread; // maximum of threads
pthread\_t \*thread_id; // a array of threads
tpool\_work\_t\* tpool_head; // tpool\_work queue
pthread\_cond\_t queue_ready; // condition varaible
pthread\_mutex\_t queue_lock; // queue lock
}tpool\_t;
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*@brief:
\* create thread pool
\*@args:
\* max\_thread\_num ---> maximum of threads
\* pool ---> address of thread pool
\*@return value:
\* 0 ---> create thread pool successfully
\* othres ---> create thread pool failed
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
int create\_tpool(tpool\_t\*\* pool,size\_t max_thread_num);
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*@brief:
\* destroy thread pool
\*@args:
\* pool ---> address of pool
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
void destroy\_tpool(tpool\_t\* pool);
/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
\*@brief:
\* add tasks to thread pool
\*@args:
\* pool ---> thread pool
\* routine ---> entry function of each thread
\* args ---> arguments
\*@return value:
\* 0 ---> add ok
\* others ---> add failed
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/
int add\_task\_2\_tpool(tpool\_t\* pool,void\* (\*routine)(void\*),void\* args);
#endif//tpool.h
需要考虑的技术问题三,线程池的所有权应该交予谁。
这里我们需要考虑到,将线程池封装成一个so库是比较好的想法,那么,线程池的所有权就应该交予调用它的函数。所以我这里采取的就是这个方法。
tpool.c
#include "tpool.h"
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
static void\* work\_routine(void\* args)
{
tpool\_t\* pool = (tpool\_t\*)args;
tpool\_work\_t\* work = NULL;
while(1){
pthread\_mutex\_lock(&pool->queue_lock);
while(!pool->tpool_head && !pool->shutdown){ // if there is no works and pool is not shutdown, it should be suspended for being awake
pthread\_cond\_wait(&pool->queue_ready,&pool->queue_lock);
}
if(pool->shutdown){
pthread\_mutex\_unlock(&pool->queue_lock);//pool shutdown,release the mutex and exit
pthread\_exit(NULL);
}
/\* tweak a work\*/
work = pool->tpool_head;
pool->tpool_head = (tpool\_work\_t\*)pool->tpool_head->next;
pthread\_mutex\_unlock(&pool->queue_lock);
work->work\_routine(work->args);
free(work);
}
return NULL;
}
int create\_tpool(tpool\_t\*\* pool,size\_t max_thread_num)
{
(\*pool) = (tpool\_t\*)malloc(sizeof(tpool\_t));
if(NULL == \*pool){
printf("in %s,malloc tpool\_t failed!,errno = %d,explain:%s\n",\_\_func\_\_,errno,strerror(errno));
exit(-1);
}
(\*pool)->shutdown = 0;
(\*pool)->maxnum_thread = max_thread_num;
(\*pool)->thread_id = (pthread\_t\*)malloc(sizeof(pthread\_t)\*max_thread_num);
if((\*pool)->thread_id == NULL){
printf("in %s,init thread id failed,errno = %d,explain:%s",\_\_func\_\_,errno,strerror(errno));
exit(-1);
}
(\*pool)->tpool_head = NULL;
if(pthread\_mutex\_init(&((\*pool)->queue_lock),NULL) != 0){
printf("in %s,initial mutex failed,errno = %d,explain:%s",\_\_func\_\_,errno,strerror(errno));
exit(-1);
}
if(pthread\_cond\_init(&((\*pool)->queue_ready),NULL) != 0){
printf("in %s,initial condition variable failed,errno = %d,explain:%s",\_\_func\_\_,errno,strerror(errno));
exit(-1);
}
for(int i = 0; i < max_thread_num; i++){
if(pthread\_create(&((\*pool)->thread_id[i]),NULL,work_routine,(void\*)(\*pool)) != 0){
printf("pthread\_create failed!\n");
exit(-1);
}
}
return 0;
}
void destroy\_tpool(tpool\_t\* pool)
{
tpool\_work\_t\* tmp_work;
if(pool->shutdown){
return;
}
pool->shutdown = 1;
pthread\_mutex\_lock(&pool->queue_lock);
pthread\_cond\_broadcast(&pool->queue_ready);
pthread\_mutex\_unlock(&pool->queue_lock);
for(int i = 0; i < pool->maxnum_thread; i++){
pthread\_join(pool->thread_id[i],NULL);
}
free(pool->thread_id);
while(pool->tpool_head){
tmp_work = pool->tpool_head;
pool->tpool_head = (tpool\_work\_t\*)pool->tpool_head->next;
free(tmp_work);
}
pthread\_mutex\_destroy(&pool->queue_lock);
pthread\_cond\_destroy(&pool->queue_ready);
free(pool);
}
int add\_task\_2\_tpool(tpool\_t\* pool,void\* (\*routine)(void\*),void\* args)
{
tpool\_work\_t\* work,\*member;
if(!routine){
printf("rontine is null!\n");
return -1;
}
work = (tpool\_work\_t\*)malloc(sizeof(tpool\_work\_t));
if(!work){
printf("in %s,malloc work error!,errno = %d,explain:%s\n",\_\_func\_\_,errno,strerror(errno));
return -1;
}
work->work_routine = routine;
work->args = args;
work->next = NULL;
pthread\_mutex\_lock(&pool->queue_lock);
member = pool->tpool_head;
if(!member){
pool->tpool_head = work;
}
else{
while(member->next){
member = (tpool\_work\_t\*)member->next;
}
member->next = work;
}
//notify the pool that new task arrived!
pthread\_cond\_signal(&pool->queue_ready);
pthread\_mutex\_unlock(&pool->queue_lock);
return 0;
}
demo.c
#include "tpool.h"
#include <stdio.h>
#include <unistd.h>
#include <time.h>
void\* fun(void\* args)
{
int thread = (int)args;
printf("running the thread of %d\n",thread);
return NULL;
}
int main(int argc, char\* args[])
{
tpool\_t\* pool = NULL;
![img](https://img-blog.csdnimg.cn/img_convert/3b6faaa892365f4e92bc7846d703e398.png)
![img](https://img-blog.csdnimg.cn/img_convert/590d08367e1aa1dbb3650d28020cf8a4.png)
![img](https://img-blog.csdnimg.cn/img_convert/1c15eced045feaae8c5617d61960c8ea.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**
)]
[外链图片转存中...(img-MAq104rH-1715519778566)]
[外链图片转存中...(img-Kn887IwL-1715519778566)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**