近日由于项目需要,需要学习一下多线程编程。在前人的代码中,我发现了一种将线程排列成一个双向链表的处理,据说这样能够管理线程间的资源冲突,对此十分怀疑,于是上网google了一把,没有找到这种用法,却找到了线程池的东西(也就是3月11日的那两篇文章),于是也想尝试一下,搞个线程池玩玩。
copy之前两篇文章中的一篇用c++实现的线程池,可惜没有编译通过,不知道是g++的问题,还是我不懂怎么编译cpp的关系。于是,把心一横,自己基于他的思想写了一个c的线程池。
所谓线程池,其核心思想就是预先开好一堆线程,然后让他们跑着,当有任务来了,便让空闲的线程去执行这些任务,完成任务后,线程又回归到之前那种等待任务的状态。
线程池的好处,对比起那种来一个任务,为其产生一个线程的做法,这样可以节省产生线程所带来的开销(这种开销当线程数达到一定数量是,相当可观)。
我的 线程池实现的思想:
1、创建一堆线程,建立好线程池。
2、用一个数组去管理任务。当任务来了后,添加到此数组,并发条件信号量唤醒等待中的线程,让线程执行任务。当线程执行完该任务后,检测任务数组中是否还有排队任务,有则继续执行任务,直到排队任务为0。
3、对外只需两个函数createthreadpool()、以及addTask()
4、实现此线程池的关键是创建线程时的线程函数Thread|Func()。在其中,要实现取任务、执行任务两大动作,其中如何取得任务最为关键(这里需要思考,如何从任务数组中找到任务,同时,在完成一个任务后,如何去找下一个排队任务,以及何时返回等待状态)
具体实现详见代码:
*******************************test.c文件
#include "cthreadpool.h"
int mytask(int taskno)
{
int i=0;
for (i=0;i<3;i++)
{
time_t timenow=time(NULL);
printf("task %d : timenow is %d run!/n",taskno,timenow);
sleep(3);
}
return 0;
}
int main(void)
{
int i=0,j=0;
printf("/n********* thread pool testing **************/n");
CThreadPool(3);
printf("main thread finish create thread pool/n");
sleep(2);
for(i=1;i<10;i++)
{
printf("main thread is adding task %d/n",i);
AddTask(i,mytask);
sleep(1);
}
while(1){
sleep(20);
printf("main thread is alive/n");
}
return 0;
}
***************************************cthreadpool.h
#ifndef __CTHREADPOOL_
#define __CTHREADPOOL_
#include <pthread.h>
#include <stdio.h>
#define MAX_THREAD 20
#define MAX_TASK 20
typedef enum STAT_ENUM
{
IDLE=0,
QUEUEING=1,
HANDLING=2,
}STAT;
typedef struct TASKLIST_STRU
{
int task;
int (*run)(int);
STAT taskStat;
}TASKLIST;
extern TASKLIST taskList[MAX_TASK];
extern int taskQueue;
extern pthread_mutex_t m_pthreadMutex;
extern pthread_cond_t m_pthreadCond;
int CThreadPool(int threadNum);
int AddTask(int task,void* taskFunc);
#endif
*********************************************cthreadpool.c
#include "cthreadpool.h"
TASKLIST taskList[MAX_TASK]={0,NULL,IDLE};
int taskQueue=0;
pthread_mutex_t m_pthreadMutex;
pthread_cond_t m_pthreadCond;
int CThreadPool(int threadNum);
void* ThreadFunc(void* threadData);
int AddTask(int task,void* taskFunc);
int CThreadPool(int threadNum)
{
int i=0;
if (threadNum > MAX_THREAD)
{
printf("can't create so much thread pool/n");
return -1;
}
for (i=0; i<threadNum; i++)
{
pthread_t tid=0;
pthread_create(&tid,NULL,ThreadFunc,NULL);
}
return 0;
}
void* ThreadFunc(void* threadData)
{
pthread_t tid = pthread_self();
int taskIndex=0;
printf("tid %lu : create success/n",tid);
while(1){
pthread_mutex_lock(&m_pthreadMutex);
printf("tid %lu : waiting, taskIndex=%d/n",tid,taskIndex);
pthread_cond_wait(&m_pthreadCond,&m_pthreadMutex);
printf("tid %lu : condition active/n",tid);
pthread_mutex_unlock(&m_pthreadMutex);
/* get task */
while (taskQueue != 0)
{
printf("tid %lu : get job, taskIndex=%d, taskQueue=%d/n",tid,taskIndex,taskQueue);
while ((taskList[taskIndex].taskStat==IDLE) || (taskList[taskIndex].taskStat==HANDLING))
{
taskIndex++;
if (taskIndex >= MAX_TASK)
{
taskIndex = 0;
}
}
/* run task */
taskQueue--;
printf("tid %lu : taskList[%d].task=%d run, taskQueue=%d/n",tid,taskIndex,taskList[taskIndex].task,taskQueue);
taskList[taskIndex].taskStat=HANDLING;
taskList[taskIndex].run(taskList[taskIndex].task);
/* delet task */
printf("tid %lu : taskList[%d].task=%d is over/n",tid,taskIndex,taskList[taskIndex].task);
taskList[taskIndex].task=0;
taskList[taskIndex].taskStat=IDLE;
printf("tid %lu : idles taskIndex=%d/n",tid,taskIndex);
}
taskIndex = 0;
}
return (void*) 0;
}
int AddTask(int task,void* taskFunc)
{
int taskIndex=0;
while (taskList[taskIndex].taskStat != IDLE)
{
taskIndex++;
if (taskIndex >= MAX_TASK)
{
printf("taskList is full, pls wait!/n");
return -1;
}
}
taskList[taskIndex].task = task;
taskList[taskIndex].taskStat = QUEUEING;
taskList[taskIndex].run = taskFunc;
taskQueue++;
printf("Adding a task, taskList[%d].task=%d, taskQueue=%d/n",taskIndex,task,taskQueue);
pthread_cond_signal(&m_pthreadCond);
return 0;
}
以上代码在rhle5.0下编译过,可以成功运行。运行结果为:
********* thread pool testing **************
tid 3085884304 : create success
tid 3085884304 : waiting, taskIndex=0
tid 3077491600 : create success
tid 3077491600 : waiting, taskIndex=0
tid 3069098896 : create success
tid 3069098896 : waiting, taskIndex=0
main thread finish create thread pool
main thread is adding task 1
Adding a task, taskList[0].task=1, taskQueue=1
tid 3085884304 : condition active
tid 3085884304 : get job, taskIndex=0, taskQueue=1
tid 3085884304 : taskList[0].task=1 run, taskQueue=0
task 1 : timenow is 1267618608 run!
main thread is adding task 2
Adding a task, taskList[1].task=2, taskQueue=1
tid 3077491600 : condition active
tid 3077491600 : get job, taskIndex=0, taskQueue=1
tid 3077491600 : taskList[1].task=2 run, taskQueue=0
task 2 : timenow is 1267618609 run!
main thread is adding task 3
Adding a task, taskList[2].task=3, taskQueue=1
tid 3069098896 : condition active
tid 3069098896 : get job, taskIndex=0, taskQueue=1
tid 3069098896 : taskList[2].task=3 run, taskQueue=0
task 3 : timenow is 1267618610 run!
task 1 : timenow is 1267618611 run!
main thread is adding task 4
Adding a task, taskList[3].task=4, taskQueue=1
task 2 : timenow is 1267618612 run!
main thread is adding task 5
Adding a task, taskList[4].task=5, taskQueue=2
task 3 : timenow is 1267618613 run!
main thread is adding task 6
Adding a task, taskList[5].task=6, taskQueue=3
task 1 : timenow is 1267618614 run!
main thread is adding task 7
Adding a task, taskList[6].task=7, taskQueue=4
task 2 : timenow is 1267618615 run!
main thread is adding task 8
Adding a task, taskList[7].task=8, taskQueue=5
task 3 : timenow is 1267618616 run!
main thread is adding task 9
Adding a task, taskList[8].task=9, taskQueue=6
tid 3085884304 : taskList[0].task=1 is over
tid 3085884304 : idles taskIndex=0
tid 3085884304 : get job, taskIndex=0, taskQueue=6
tid 3085884304 : taskList[3].task=4 run, taskQueue=5
task 4 : timenow is 1267618617 run!
tid 3077491600 : taskList[1].task=2 is over
tid 3077491600 : idles taskIndex=1
tid 3077491600 : get job, taskIndex=1, taskQueue=5
tid 3077491600 : taskList[4].task=5 run, taskQueue=4
task 5 : timenow is 1267618618 run!
tid 3069098896 : taskList[2].task=3 is over
tid 3069098896 : idles taskIndex=2
tid 3069098896 : get job, taskIndex=2, taskQueue=4
tid 3069098896 : taskList[5].task=6 run, taskQueue=3
task 6 : timenow is 1267618619 run!
task 4 : timenow is 1267618620 run!
task 5 : timenow is 1267618621 run!
task 6 : timenow is 1267618622 run!
task 4 : timenow is 1267618623 run!
task 5 : timenow is 1267618624 run!
task 6 : timenow is 1267618625 run!
tid 3085884304 : taskList[3].task=4 is over
tid 3085884304 : idles taskIndex=3
tid 3085884304 : get job, taskIndex=3, taskQueue=3
tid 3085884304 : taskList[6].task=7 run, taskQueue=2
task 7 : timenow is 1267618626 run!
tid 3077491600 : taskList[4].task=5 is over
tid 3077491600 : idles taskIndex=4
tid 3077491600 : get job, taskIndex=4, taskQueue=2
tid 3077491600 : taskList[7].task=8 run, taskQueue=1
task 8 : timenow is 1267618627 run!
tid 3069098896 : taskList[5].task=6 is over
tid 3069098896 : idles taskIndex=5
tid 3069098896 : get job, taskIndex=5, taskQueue=1
tid 3069098896 : taskList[8].task=9 run, taskQueue=0
task 9 : timenow is 1267618628 run!
task 7 : timenow is 1267618629 run!
task 8 : timenow is 1267618630 run!
task 9 : timenow is 1267618631 run!
task 7 : timenow is 1267618632 run!
task 8 : timenow is 1267618633 run!
task 9 : timenow is 1267618634 run!
tid 3085884304 : taskList[6].task=7 is over
tid 3085884304 : idles taskIndex=6
tid 3085884304 : waiting, taskIndex=0
tid 3077491600 : taskList[7].task=8 is over
tid 3077491600 : idles taskIndex=7
tid 3077491600 : waiting, taskIndex=0
main thread is alive
tid 3069098896 : taskList[8].task=9 is over
tid 3069098896 : idles taskIndex=8
tid 3069098896 : waiting, taskIndex=0
main thread is alive
后记:
这个线程池显然十分简单,还有很多不足的地方:
1、没有一个线程池的管理函数,线程池没有动态管理线程的功能(忙时多开线程,闲时关闭部分空闲线程)
2、任务用数组管理缺乏灵活性,若改成双向链表则更好
3、这段代码没有对空闲线程、繁忙线程做一个状态统计,不能查询哪个线程正在做什么任务。
4、所执行的任务接口函数,被固定了,不利于使用,需要更加灵活些。
Anyway,这是我第一个的线程池实现,还是比较高兴的,我想还是有很多不足的,希望看到这篇文章的朋友不吝赐教~~~