【学习记录】c语言线程池

最近在研究c语言的后端http服务器的时候遇到了一个问题,一般来说前端发起一个请求,收到之后建立连接新建线程处理请求,结束之后就关闭线程,流程没有什么问题,但是当请求很多,处理又比较简单的时候,新建线程就显得很费,所以引入线程池的方式来减少创建线程的开销。

线程池(Thread Pool)是一种并发编程中常用的技术,用于管理和重用线程。它通过减少线程的创建和销毁次数,以及控制并发执行的线程数量,来提高系统的性能和资源利用率。

以下是具体实现过程:
.c文件

#include <cstdio>

extern "C"{
#include "threadPool.h"


/**
 *@brief 搜索待执行的任务
 *@param callback 返回的回调函数指针
 *@return 如果找到了就返回对应的下标
 *
 * */
int thread_poolsearch(void (**callback)(void *),thread_pool * mthpool){
    if(mthpool->inited==0) return -1; //线程池未初始化
    for (int i = 0; i < MAX_THREADNUM; i++) {
        if(mthpool->tasklistlist[i].used==1){
            *callback = mthpool->tasklistlist[i].task_run;
            mthpool->tasklistlist[i].used=0;
            return i;
        }
    }
    return -1;
}


/**
 *@brief 线程池中插入
 *@param callback 要运行的函数
 *@param arg 函数传入的参数
 *@param mthpool 线程池
 *
 * */
int thread_pooltaskpush(void (*callback)(void *),void * arg,thread_pool * mthpool){
    if(mthpool->task_busy >=MAX_THREADNUM) return -1;
    pthread_mutex_lock(&mthpool->jobs_mutex);//临界段

    for (int i = 0; i < MAX_THREADNUM; i++) {
        if(mthpool->tasklistlist[i].used==0){
            mthpool->tasklistlist[i].task_run = callback;
            mthpool->tasklistlist[i].arg = arg;
            pthread_cond_signal(&mthpool->jobs_cond); //产生信号
            mthpool->tasklistlist[i].used =1;
            pthread_mutex_unlock(&mthpool->jobs_mutex);
            return 0;
        }
    }
    pthread_mutex_unlock(&mthpool->jobs_mutex);
    return -1; //满了
}

static void* threadpool_callback(void* arg) {
    thread_pool * mthpool_thread = (thread_pool *)arg;
    void (*thread_run)(void *) =NULL;
    int ret=0;
    printf("线程开启\n");
    while (1){
        pthread_mutex_lock(&mthpool_thread->jobs_mutex);

        printf("线程等待信号量 进入阻塞\n");
        pthread_cond_wait(&mthpool_thread->jobs_cond, &mthpool_thread->jobs_mutex);
        printf("线程收到信号量 解除阻塞\n");
        ret = thread_poolsearch(&thread_run,mthpool_thread);
        printf("线程收到信号量 解除阻塞 ret=%d\n",ret);
        if(ret>=0){
            mthpool_thread->task_busy++; //运行前busy数量增加 1
        }
        pthread_mutex_unlock(&mthpool_thread->jobs_mutex);
        if(ret>=0){
            printf("线程收到信号量 解除阻塞 执行\n");
            thread_run(mthpool_thread->tasklistlist[ret].arg);
        }
        pthread_mutex_lock(&mthpool_thread->jobs_mutex); //重新锁定减少占用
        mthpool_thread->task_busy--;
        pthread_mutex_unlock(&mthpool_thread->jobs_mutex);

    }

    pthread_exit(NULL);

}

//线程池初始化
int thread_poolinit(thread_pool * mthpool){
    mthpool->jobs_mutex = PTHREAD_MUTEX_INITIALIZER;
    mthpool->jobs_cond = PTHREAD_COND_INITIALIZER;
    for (int i = 0; i < MAX_THREADNUM; i++) {
        int ret = pthread_create(&mthpool->pthreadhandle[i], NULL,threadpool_callback, mthpool);
        if (ret) {
            return -1;
        }
    }
//    mthpool->task_busy=MAX_THREADNUM;
    mthpool->inited=1;
    return 0;
}




.h文件

#ifndef _THREADPOOL_H
#define _THREADPOOL_H
extern "C"{
#include "pthread.h"
#define MAX_THREADNUM 10



struct tasklistnode{
    void (*task_run)(void *arg);
    void * arg;
    int used; //1-有值待执行 0-执行结束或无值
};

struct thread_poolnode{
    int inited;
    pthread_cond_t jobs_cond;
    pthread_mutex_t jobs_mutex;
    struct tasklistnode tasklistlist[MAX_THREADNUM];
    pthread_t pthreadhandle[MAX_THREADNUM];
    int task_busy; //已创建线程中busy的线程个数
};
typedef struct tasklistnode tasklist;
typedef struct thread_poolnode thread_pool;



//线程池初始化
int thread_poolinit(thread_pool * mthpool);

/**
 *@brief 线程池中插入
 *@param callback 要运行的函数
 *@param arg 函数传入的参数
 *@param mthpool 线程池
 * */
int thread_pooltaskpush(void (*callback)(void *),void * arg,thread_pool * mthpool);

使用示例:

     thread_pool dasda1={0};
    thread_poolinit(&dasda1);
    thread_pooltaskpush(print_hello,&dasda1,&dasda1);
    thread_pooltaskpush(print_hello2,&dasda1,&dasda1);
    thread_pooltaskpush(print_hello3,&dasda1,&dasda1);
    thread_pooltaskpush(print_hello3,&dasda1,&dasda1);
    thread_pooltaskpush(print_hello3,&dasda1,&dasda1);
    thread_pooltaskpush(print_hello3,&dasda1,&dasda1);
    thread_pooltaskpush(print_hello3,&dasda1,&dasda1);
    while (1){
        sleep(1);
    }

参考资料:【1】
【2】

2024-12-11 更新发现一个更简洁好用的c 线程池实现

原文链接
源码如下:
condition.h

#ifndef _CONDITION_H_
#define _CONDITION_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <pthread.h>

//封装一个互斥量和条件变量作为状态
typedef struct condition
{
    pthread_mutex_t pmutex;
    pthread_cond_t pcond;
}condition_t;

//对状态的操作函数
int condition_init(condition_t *cond);
int condition_lock(condition_t *cond);
int condition_unlock(condition_t *cond);
int condition_wait(condition_t *cond);
int condition_timedwait(condition_t *cond, const struct timespec *abstime);
int condition_signal(condition_t* cond);
int condition_broadcast(condition_t *cond);
int condition_destroy(condition_t *cond);
#ifdef __cplusplus
}
#endif
#endif

condition.c

#include "condition.h"

//初始化
int condition_init(condition_t *cond)
{
    int status;
    if((status = pthread_mutex_init(&cond->pmutex, NULL)))
        return status;

    if((status = pthread_cond_init(&cond->pcond, NULL)))
        return status;

    return 0;
}

//加锁
int condition_lock(condition_t *cond)
{
    return pthread_mutex_lock(&cond->pmutex);
}

//解锁
int condition_unlock(condition_t *cond)
{
    return pthread_mutex_unlock(&cond->pmutex);
}

//等待
int condition_wait(condition_t *cond)
{
    return pthread_cond_wait(&cond->pcond, &cond->pmutex);
}

//固定时间等待
int condition_timedwait(condition_t *cond, const struct timespec *abstime)
{
    return pthread_cond_timedwait(&cond->pcond, &cond->pmutex, abstime);
}

//唤醒一个睡眠线程
int condition_signal(condition_t* cond)
{
    return pthread_cond_signal(&cond->pcond);
}

//唤醒所有睡眠线程
int condition_broadcast(condition_t *cond)
{
    return pthread_cond_broadcast(&cond->pcond);
}

//销毁互斥锁和条件变量
int condition_destroy(condition_t *cond)
{
    int status;
    if((status = pthread_mutex_destroy(&cond->pmutex)))
        return status;

    if((status = pthread_cond_destroy(&cond->pcond)))
        return status;

    return 0;
}

threadpool.h

#ifndef _THREAD_POOL_H_
#define _THREAD_POOL_H_
#ifdef __cplusplus
extern "C" {
#endif
//需要引入状态的头文件--条件变量和互斥锁
#include "condition.h"

//封装线程池中的对象需要执行的任务对象
typedef struct task
{
    void *(*run)(void *args);  //函数指针,需要执行的任务
    void *arg;              //参数
    struct task *next;      //任务队列中下一个任务
}task_t;


//下面是线程池结构体
typedef struct threadpool
{
    condition_t ready;    //状态量
    task_t *first;       //任务队列中第一个任务
    task_t *last;        //任务队列中最后一个任务
    int counter;         //线程池中已有线程数
    int idle;            //线程池中空闲线程数
    int max_threads;     //线程池最大线程数
    int quit;            //是否退出标志 1/0
}threadpool_t;


//线程池初始化
void threadpool_init(threadpool_t *pool, int idle_threads, int max_threads);

//往线程池中加入任务
int threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg);

//摧毁线程池
void threadpool_destroy(threadpool_t *pool);
#ifdef __cplusplus
}
#endif
#endif

threadpool.c

#include "threadpool.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <time.h>

//线程池中创建的线程执行
void *thread_routine(void *arg)
{
    struct timespec abstime;//时间结构体
    int timeout;
    printf("thread %d is starting\n", (int)pthread_self());
    threadpool_t *pool = (threadpool_t *)arg;

    //死循环使线程池中空闲的线程可以复用
    while(1)
    {
        timeout = 0;

        //访问线程池之前需要加锁
        condition_lock(&pool->ready);

        //空闲
        pool->idle++;

        //任务队列没有任务到来并且没有收到线程池销毁通知, 线程阻塞等待(进入这里面都是空闲线程,等待被唤醒)
        while(pool->first == NULL && !pool->quit)
        {
            printf("thread %d is waiting\n", (int)pthread_self());

            //获取当前时间,并加上等待时间, 从而设置进程的超时睡眠时间
            clock_gettime(CLOCK_REALTIME, &abstime);
            abstime.tv_sec += 2;
            int status;
            status = condition_timedwait(&pool->ready, &abstime);  //该函数会解锁,允许其他线程访问,当被唤醒时,加锁
            if(status == ETIMEDOUT)
            {
                printf("thread %d wait timed out\n", (int)pthread_self());
                timeout = 1;
                break;
            }
        }

        pool->idle--;
        if(pool->first != NULL)
        {
            //取出任务队列最前的任务,移除任务,并执行任务
            task_t *t = pool->first;
            pool->first = t->next;
            //由于任务执行需要消耗时间,先解锁让其他线程访问线程池
            condition_unlock(&pool->ready);
            //执行任务
            t->run(t->arg);
            //执行完任务释放内存
            free(t);
            //重新加锁
            condition_lock(&pool->ready);
        }

        //退出线程池--销毁当前线程
        if(pool->quit && pool->first == NULL)
        {
            pool->counter--;//当前工作的线程数-1

            //若线程池中没有线程,唤醒等待线程(主线程--销毁线程池的线程)全部任务已经完成
            if(pool->counter == 0)
            {
                condition_signal(&pool->ready);
            }
            condition_unlock(&pool->ready);
            break;
        }
        //超时,说明线程没有任务可以执行, 跳出销毁线程
        if(timeout == 1)
        {
            pool->counter--;//当前工作的线程数-1
            condition_unlock(&pool->ready);
            break;
        }

        condition_unlock(&pool->ready);
    }

    printf("thread %d is exiting\n", (int)pthread_self());
    return NULL;

}


//线程池初始化
void threadpool_init(threadpool_t *pool, int idle_threads, int max_threads)
{

    condition_init(&pool->ready);
    pool->first = NULL;
    pool->last =NULL;
    pool->counter =0;
    pool->idle =0;
    pool->max_threads = max_threads;
    pool->quit =0;

    //创建空闲线程
    int i = 0;
    for(; i < idle_threads; i++)
    {
        pthread_t tid;
        pthread_create(&tid, NULL, thread_routine, pool);
        pool->counter++;//已有线程数+1
    }

}
//增加一个任务到线程池
int threadpool_add_task(threadpool_t *pool, void *(*run)(void *arg), void *arg)
{
    //产生一个新的任务
    task_t *newtask = (task_t *)malloc(sizeof(task_t));
    newtask->run = run;
    newtask->arg = arg;
    newtask->next=NULL;//新加的任务放在队列尾端

    //线程池的状态被多个线程共享,操作前需要加锁
    condition_lock(&pool->ready);

    if(pool->first == NULL)//第一个任务加入
    {
        pool->first = newtask;
    }
    else
    {
        pool->last->next = newtask;
    }
    pool->last = newtask;  //队列尾指向新加入的线程

    //线程池中有线程空闲,唤醒处于等待状态的线程(因为在等待期间会释放互斥锁)
    if(pool->idle > 0)
    {
        condition_signal(&pool->ready);
    }
        //当前线程池中线程个数没有达到设定的最大值,创建一个新的线程
    else if(pool->counter < pool->max_threads)
    {
        pthread_t tid;
        pthread_create(&tid, NULL, thread_routine, pool);
        pool->counter++;
    }
    else
    {
        condition_unlock(&pool->ready);
        return -1;
    }
    //结束,访问
    condition_unlock(&pool->ready);
    return 0;
}

//线程池销毁
void threadpool_destroy(threadpool_t *pool)
{
    //如果已经调用销毁,直接返回
    if(pool->quit)
    {
        return;
    }
    //加锁
    condition_lock(&pool->ready);
    //设置销毁标记为1
    pool->quit = 1;
    //线程池中线程个数大于0
    if(pool->counter > 0)
    {
        //对于等待的线程,发送信号唤醒
        if(pool->idle > 0)
        {
            condition_broadcast(&pool->ready);
        }
        //正在执行任务的线程,等待他们结束任务
        while(pool->counter)
        {
            condition_wait(&pool->ready);
        }
    }
    condition_unlock(&pool->ready);
    condition_destroy(&pool->ready);
}


测试代码

#include <stdio.h>
#include "threadpool.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
void* mytask(void *arg)
{
    int i = *((int*)(arg));

    printf("thread %d is working on task %d\n", (int)pthread_self(),i);
    sleep(i+1);

}
//测试代码
int main(void)
{
    threadpool_t pool;
    //初始化线程池,创建2个空闲线程,最多5个线程
    threadpool_init(&pool, 2, 2);
    int i;
    int data[10]={0};
    //创建十个任务
    for(i=0; i < 10; i++)
    {
        data[i]=i;
        threadpool_add_task(&pool, mytask, &data[i]);

    }
    printf("完毕\n");

    threadpool_destroy(&pool);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值