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