既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
#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;
if(0 != create\_tpool(&pool,5)){
printf("create\_tpool failed!\n");
return -1;
}
for(int i = 0; i < 1000; i++){
add\_task\_2\_tpool(pool,fun,(void\*)i);
}
sleep(2);
destroy\_tpool(pool);
return 0;
}
Makefile
pool: tpool.c demo.c
gcc tpool.c demo.c -o pool -lpthread -std=c99
clean:
rm ./pool
其他的一些C语言实现可以参考下面这些文章:
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
[外链图片转存中…(img-ARFlQMVU-1715823376855)]
[外链图片转存中…(img-jHhAzGem-1715823376856)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新