linux C 线程池(独占共享锁实现)

本文介绍了一种基于共享独占模式的线程池实现方案,适用于任务执行频繁但短暂的场景。通过C语言实现了一个简单的线程池,并提供了初始化、添加任务及销毁线程池的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果一个应用需要频繁的创建和销毁线程,而任务执行的时间又非常短,这样线程创建和销毁的带来的开销就不容忽视,这时也是线程池该出场的机会了。如果线程创建和销毁时间相比任务执行时间可以忽略不计,则没有必要使用线程池了,本文使用共享独占模式实现线程池中任务同步与互斥。使用场景主要用于写入任务较少,执行任务较多的场景。

/////////////////////multiThreadTest.c////////////////////////

#include "multiThreadTest.h"

typedef struct ThreadWorker
{
    void *(*TaskRun)(void *arg);
    void *arg;
    struct ThreadWorker *next;
};
typedef struct ThreadPool
{
    pthread_rwlock_t rwlock;
    struct ThreadWorker *queueHead;
   // struct ThreadWorker *freeResource;
    int shutdown;
    pthread_t *threadid;
    int maxThreadNum;
    int curQueueSize;
    //int curFreeResourceSize;
};
static struct ThreadPool *pool=NULL;
static void *ThreadRun(void *arg);
void poolInit(int maxThreadNum)
{
    pool=(struct ThreadPool *)malloc(sizeof(struct ThreadPool));
    pthread_rwlock_init(&(pool->rwlock),NULL);
    pool->queueHead=NULL;
    pool->maxThreadNum=maxThreadNum;
    pool->curQueueSize=0;
    pool->shutdown=0;
    pool->threadid=(pthread_t *)malloc(maxThreadNum*sizeof(pthread_t));
    int i=0;
    pthread_setconcurrency(maxThreadNum);//兼容问题
    for(i=0;i<maxThreadNum;i++)
    {
        pthread_create(&(pool->threadid[i]),NULL,ThreadRun,NULL);
    }
}
int PoolAddTask(void *(*TaskRun)(void *arg),void *arg)
{
    struct ThreadWorker *newTask=(struct ThreadWorker *)malloc(sizeof(struct ThreadWorker ));
    newTask->TaskRun=TaskRun;
    newTask->arg=arg;
    newTask->next=NULL;
    pthread_rwlock_wrlock(&(pool->rwlock));
    struct ThreadWorker *member=pool->queueHead;
    if(member!=NULL)
    {
        while(member->next!=NULL)
        {
            member=member->next;
        }
        member->next=newTask;
    }
    else
    {
        pool->queueHead=newTask;
    }
    if(pool->queueHead!=NULL)
    {
        pool->curQueueSize++;
    }
    pthread_rwlock_unlock(&(pool->rwlock));
    return 0;
}
static void *ThreadRun(void *arg)
{
    struct ThreadWorker *curWorker=NULL;
    int shutdown=0;
    while(1)
    {
        pthread_rwlock_rdlock(&(pool->rwlock));
        shutdown=pool->shutdown;
        if(pool->curQueueSize!=0&&pool->queueHead!=NULL)
        {
            pool->curQueueSize--;
            curWorker=pool->queueHead;
            pool->queueHead=curWorker->next;
        }
        pthread_rwlock_unlock(&(pool->rwlock));
        if(curWorker==NULL&&shutdown!=0)//任务链表为空且收到销毁线池命令时终止线程
        {
            break;
        }
        if(curWorker!=NULL)
        {
            (*(curWorker->TaskRun))(curWorker->arg);
            free(curWorker);
            curWorker=NULL;
        }
    }
    pthread_exit(0);
}
int PoolDestroy()
{
    if(pool->shutdown)
    {
        return -1;
    }
    pthread_rwlock_wrlock(&(pool->rwlock));
    pool->shutdown=1;
    pthread_rwlock_unlock(&(pool->rwlock));
    int i;
    for(i=0;i<pool->maxThreadNum;i++)
    {
        pthread_join(pool->threadid[i],NULL);
    }
    free(pool->threadid);
    struct ThreadWorker *deleteHeader=NULL;
    while(pool->queueHead!=NULL)
    {
        deleteHeader=pool->queueHead;
        pool->queueHead=pool->queueHead->next;
        free(deleteHeader);
    }
    pthread_rwlock_destroy(&(pool->rwlock));
    free(pool);
    return 0;
}




/////////////////////multiThreadTest.h////////////////////////


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <assert.h>
void poolInit(int maxThreadNum);
int PoolAddTask(void *(*TaskRun)(void *arg),void *arg);
int PoolDestroy();
/////////////////////test.c////////////////////////

#include "multiThreadTest.h"
void *myfunc(void *arg);
int main(void)
{
    poolInit(10);
    int *num=(int *)malloc(sizeof(int)*100);
    int i=0;
    for(i=0;i<100;i++)
    {
        num[i]=i;
        PoolAddTask(myfunc,&num[i]);
    }
    PoolDestroy();
    free(num);
    exit(0);
}
void *myfunc(void *arg)
{
    printf("task %d start!\n",*((int *)arg));
    sleep(10);
    return (void *)0;
}
/////

编译参数: gcc -o test  multiThreadTest.* test.c -lrt -lpthread

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值