原理
在多线程程序中如果频繁的创建和结束一个线程这样会使系统的性能降低,这时我们可以创建一个线程 池来完成这些任务执行完后让其阻塞等待其他的任务这样就可以提高系统的性能
一个线程池要包括以下几部分
1、线程池管理器(ThreadPool):用于创建并管理线程池,包括 创建线程池,销毁线程池,添加新任务;
2、工作线程(PoolWorker):线程池中线程,在没有任务时处于等待状态,可以循环的执行任务;
3、任务接口(Task):每个任务必须实现的接口,以供工作线程调度任务的执行,它主要规定了任务的入口,任务执行完后的收尾工作,任务的执行状态等;
4、任务队列(taskQueue):用于存放没有处理的任务。提供一种缓冲机制。
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int pool_add_worker(void*(process)(void* arg),void *arg);
void *thread_routine(void* arg);
struct pthread_worker
{
pthread_worker(void *(*pro)(void* arg),void *argg)
:process(pro)
,arg(argg)
,next(NULL)
{}
void *(*process)(void* arg);
void *arg;
pthread_worker* next;
};
struct thread_pool
{
thread_pool(int max)
:shutdown(0)
,max_thread_num(max)
,cur_list_size(0)
,list_head(NULL)
,thread_id(new pthread_t[max])
{
pthread_mutex_init(&lock,NULL);
pthread_cond_init(&ready,NULL);
for (size_t i=0; i<max; ++i)
{
pthread_create(&thread_id[i],NULL,thread_routine,NULL);
cout<<thread_id[i]<<endl;
}
}
~thread_pool()
{
if(!shutdown)
{
while(cur_list_size)
{
sleep(1);
}
shutdown=1;
pthread_cond_broadcast(&ready);
for(size_t i = 0; i < max_thread_num; ++i)
{
cout<<thread_id[i]<<endl;
pthread_join(thread_id[i],NULL);
}
delete[] thread_id;
while(list_head != NULL)
{
pthread_worker* head = NULL;
head=list_head;
list_head = list_head->next;
delete head;
}
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&ready);
}
}
pthread_mutex_t lock;
pthread_cond_t ready;
int shutdown;
int max_thread_num;
int cur_list_size;
pthread_worker* list_head;
pthread_t* thread_id;
};
thread_pool* pool=NULL;
int pool_add_worker(void *(*process)(void* arg), void* arg)
{
pthread_worker* newworker=new pthread_worker(process,arg);
pthread_mutex_lock(&(pool->lock));
pthread_worker* head=pool->list_head;
if(head)
{
while(head->next)
head = head->next;
head->next = newworker;
}
else
pool->list_head = newworker;
assert(pool->list_head != NULL);
pool->cur_list_size++;
pthread_mutex_unlock(&(pool->lock));
pthread_cond_signal(&(pool->ready));
return 0;
}
void* thread_routine(void* arg)
{
cout<<pthread_self()<<"thread is created"<<endl;
while(true)
{
cout<<pool->cur_list_size<<endl;
pthread_mutex_lock(&(pool->lock));
while(pool->cur_list_size == 0 && !pool->shutdown)
{
cout<<pthread_self()<<"is waiting..."<<endl;
pthread_cond_wait(&(pool->ready),&(pool->lock));
}
if(pool->shutdown)
{
pthread_mutex_unlock(&(pool->lock));
cout<<pthread_self()<<"will exit"<<endl;
sleep(1);
pthread_exit(NULL);
}
cout<<pthread_self()<<"is runinng.."<<endl;
sleep(1);
assert(pool->cur_list_size != 0);
assert(pool->list_head != NULL);
pool->cur_list_size--;
pthread_worker* worker = pool->list_head;
pool->list_head = worker->next;
pthread_mutex_unlock(&(pool->lock));
(*(worker->process))(worker->arg);
delete worker;
worker = NULL;
}
pthread_exit(NULL);
}
void* myprocess(void* arg)
{
cout<<"thread"<<pthread_self()<<"is working on task"<<(int)arg<<endl;
sleep(1);
return NULL;
}
int main()
{
int size;
cin>>size;
pool = new thread_pool(3);
sleep(3);
for(size_t i = 0; i < size; ++i)
{
pool_add_worker(myprocess,(void*)i);
}
sleep(10);
delete pool;
return 0;
}