linux c 线程池(互斥变量+条件变量)

本文介绍了一个简单的线程池实现方法,包括线程池结构定义、任务分配和销毁流程等核心部分,并通过一个具体的例子展示了如何使用该线程池进行多线程任务调度。

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

////////////////////////////// 线程池主要实现模块multiThreadTest.c////////////////////////////////////////////////////////

#include <stdio.h>

#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <assert.h>
typedef struct ThreadWorker
{
    void *(*TaskRun)(void *arg);
    void *arg;
    struct ThreadWorker *next;
};
typedef struct ThreadPool
{
   // pthread_rwlock_t rwlock;
    pthread_cond_t cond;
    pthread_mutex_t mutex;
    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);
    pthread_cond_init(&(pool->cond),NULL);
    pthread_mutex_init(&(pool->mutex),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));
    pthread_mutex_lock(&(pool->mutex));
    printf("add Task to queue!\n");
    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_mutex_unlock(&(pool->mutex));
    pthread_cond_signal(&(pool->cond));
    //pthread_rwlock_unlock(&(pool->rwlock));
    return 0;
}
static void *ThreadRun(void *arg)
{
    struct ThreadWorker *curWorker=NULL;
    int shutdown=0;
    while(1)
    {
        pthread_mutex_lock(&(pool->mutex));
        while(pool->curQueueSize==0&&!pool->shutdown)
        {
            pthread_cond_wait(&(pool->cond),&(pool->mutex));
        }
        //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_mutex_unlock(&(pool->mutex));
        //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));
    pthread_mutex_lock(&(pool->mutex));
    pool->shutdown=1;
    pthread_mutex_unlock(&(pool->mutex));
   //pthread_rwlock_unlock(&(pool->rwlock));
    pthread_cond_broadcast(&(pool->cond));
    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_mutex_destroy(&(pool->mutex));
    pthread_cond_destroy(&(pool->cond));
   // 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 <stdio.h>
#include <math.h>
#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)
{
    int sleepTime=rand()%20;
    sleep(sleepTime);
    printf("task %d start!sleep time %d\n",*((int *)arg),sleepTime);
    return (void *)0;
}
/////////////////////////////编译说明///////////////////////////////////////////////

正式编译 gcc -o test multiThreadTest.* test.c -lrt -lpthread

gdb调试编译  gcc -g  multiThreadTest.* test.c -o test -lrt -lpthread

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值