1: 线程池组件的配置文件:

[log]
;DEBUG = 0
;INFO = 1
;WARN = 2
;FAULT = 3
level=0 ;SCREENOUT = 0
;FILEOUT = 1
;BOTH = 2
director = 2 ;TEST = 0
;RUN = 1
mode = 0 ;ONE = 0x00
;FOUR = 0x01
split = 0 ;AUTODEL = 0x00
;MANUALDEL = 0x01
manager=0 [threadpool]
minthread = 10
maxthread = 25
maxjobsize = 100000
keepalive = 1

2:线程池的代码:

#pragma once

#include "ace/Task.h"
#include "ace/Synch.h" class CManager; class CWorker : public ACE_Task<ACE_MT_SYNCH>
{
public:
CWorker(void);
CWorker(CManager* pmanager);
~CWorker(void);
public:
virtual int open();
virtual int svc();
virtual int close();
void output();
void setidletimepoint();
bool gettimervalid();
int addtimer();
int removetimer();
void increaseinvokenumber();
int getinvokenumber();
int handle_timeout(const ACE_Time_Value &tv, const void *arg);
private:
ACE_thread_t threads[0x01];
CManager* m_pmanager;
public:
int m_idletimepoint;
int getidletimelength();
int getthreadid();
int m_timerid;
int m_invokenumber;
static int m_count;
bool timervalid;
}; #include "Manager.h"
#include "Worker.h"
#include "Job.h"
#include "ace/Reactor.h"
#include "Logger.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_unistd.h" int CWorker::m_count = 0; CWorker::CWorker(void)
{
m_count++;
} CWorker::CWorker(CManager* pmanager)
{
CLogger::createinstance()->logdebugmsg("(+%d)worker address=%08x\n", m_count, this);
m_count++;
m_pmanager = pmanager;
m_timerid = -1;
m_invokenumber = 0;
timervalid = false; } CWorker::~CWorker(void)
{
m_count--;
CLogger::createinstance()->logdebugmsg("(-%d)worker address=%08x\n", m_count, this);
} int CWorker::open()
{
return activate(THR_NEW_LWP|THR_CANCEL_ENABLE|THR_JOINABLE, 1, 0, ACE_DEFAULT_THREAD_PRIORITY, -1, this, 0, 0, 0, threads);
} int CWorker::svc()
{
if (ACE_Thread::self() == threads[0])
{
ACE_Thread_Manager *mgr = this->thr_mgr();
while(true)
{
ACE_Message_Block *mb;
int result = this->getq(mb);
if (result == -1)
{
break;
}
else
{
CJob *pjob = (CJob*)mb->base();
pjob->run();
mb->release();
m_pmanager->put(this);
}
}
}
return 0;
} int CWorker::close()
{
return 0;
} void CWorker::setidletimepoint()
{
m_idletimepoint = time(NULL);
} int CWorker::getidletimelength()
{
return (int)(time(NULL) - m_idletimepoint);
} int CWorker::getthreadid()
{
return (int)threads[0];
} void CWorker::output()
{
CLogger::createinstance()->logfaultmsg("id=%05d idletime=%d invoke=%d\n", getthreadid(), getidletimelength(), getinvokenumber());
} int CWorker::addtimer()
{
ACE_Time_Value tv(m_pmanager->getkeepalivetime(), 0);
m_timerid = ACE_Reactor::instance()->schedule_timer(this, NULL, tv);
return m_timerid;
} int CWorker::removetimer()
{
return ACE_Reactor::instance()->cancel_timer(m_timerid);
} bool CWorker::gettimervalid()
{
return timervalid;
} int CWorker::handle_timeout(const ACE_Time_Value &tv, const void *arg)
{
if (m_pmanager->getthreadnumber() > m_pmanager->getcorepoolsize())
{
timervalid = true;
m_pmanager->reducethreadnumber();
this->msg_queue()->deactivate();
wait();
m_pmanager->recyclebin(this);
delete this;
}
return 0;
} void CWorker::increaseinvokenumber()
{
m_invokenumber++;
} int CWorker::getinvokenumber()
{
return m_invokenumber;
}
#pragma once

#include "list"
#include "ace/Synch.h" using namespace std; class CWorker;
class CManager; class CWorkerlist
{
typedef list<CWorker*> IDLEWORKERLIST;
typedef list<CWorker*> BUSYWORKERLIST;
public:
CWorkerlist(void);
~CWorkerlist(void);
private:
IDLEWORKERLIST m_idleworkerlist;
BUSYWORKERLIST m_busyworkerlist;
private:
ACE_Thread_Mutex m_mutex;
ACE_Condition<ACE_Thread_Mutex> *m_pcondition;
public:
CWorker* get();
void put(CWorker* pworker);
int recyclebin(CWorker* pworker);
void getsize(int* idle, int* busy);
void output();
}; #include "Workerlist.h"
#include "Worker.h"
#include "Manager.h"
#include "logger.h" CWorkerlist::CWorkerlist(void)
{
m_pcondition = new ACE_Condition<ACE_Thread_Mutex>(m_mutex);
} CWorkerlist::~CWorkerlist(void)
{
delete m_pcondition;
} CWorker* CWorkerlist::get()
{
m_mutex.acquire();
while(m_idleworkerlist.size() == 0)
{
m_pcondition->signal();
m_pcondition->wait();
}
CWorker* pworker = NULL;
pworker = m_idleworkerlist.front();
//删除定时器
pworker->removetimer();
pworker->increaseinvokenumber(); m_idleworkerlist.pop_front();
m_busyworkerlist.push_back(pworker);
m_mutex.release();
return pworker;
} int CWorkerlist::recyclebin(CWorker* pworker)
{ #define NOT_FOUND 0x00
#define IN_IDLE_QUEUE 0x01
#define IN_BUSY_QUEUE 0x02 int result = NOT_FOUND; m_mutex.acquire(); typedef list<CWorker*>::iterator ITERATOR;
ITERATOR LI; for(LI = m_idleworkerlist.begin(); LI != m_idleworkerlist.end(); LI++)
{
if ((*LI) == pworker)
{
result = IN_IDLE_QUEUE;
m_idleworkerlist.erase(LI);
m_mutex.release();
return result;
}
} for(LI = m_busyworkerlist.begin(); LI != m_busyworkerlist.end(); LI++)
{
if ((*LI) == pworker)
{
result = IN_BUSY_QUEUE;
m_idleworkerlist.erase(LI);
m_mutex.release();
return result;
}
} m_mutex.release();
return result;
} void CWorkerlist::put(CWorker* pworker)
{
m_mutex.acquire(); typedef list<CWorker*>::iterator ITERATOR;
ITERATOR LI; for(LI = m_busyworkerlist.begin(); LI != m_busyworkerlist.end(); LI++)
{
if (*LI == pworker)
{
(*LI)->removetimer();
//空闲定时器已经生效,开始走删除该工作线程的流程了,所以保持在忙的队列中。
if(pworker->gettimervalid() != true)
{
m_busyworkerlist.erase(LI);
}
break;
}
} if(pworker->gettimervalid() != true)
{
//启动定时器
pworker->setidletimepoint();
pworker->addtimer(); m_idleworkerlist.push_back(pworker);
m_pcondition->signal();
}
m_mutex.release();
} void CWorkerlist::getsize(int *idle, int *busy)
{
m_mutex.acquire();
*idle = m_idleworkerlist.size();
*busy = m_busyworkerlist.size();
m_mutex.release();
} void CWorkerlist::output()
{
m_mutex.acquire(); typedef list<CWorker*>::iterator ITERATOR;
ITERATOR LI; CLogger::createinstance()->logfaultmsg("idle thread information as follows\n");
int idle = 0;
for(LI = m_idleworkerlist.begin(); LI != m_idleworkerlist.end(); LI++)
{
idle++;
(*LI)->output();
} CLogger::createinstance()->logfaultmsg("busy thread information as follows\n");
int busy = 0;
for(LI = m_busyworkerlist.begin(); LI != m_busyworkerlist.end(); LI++)
{
busy++;
(*LI)->output();
} CLogger::createinstance()->logfaultmsg("idle=%d busy=%d\n", idle, busy); m_mutex.release();
}
#pragma once

#include "ace/Task.h"
#include "ace/Synch.h"
#include "Job.h"
#include "Workerlist.h" class CWorker; class CManager : public ACE_Task<ACE_MT_SYNCH>
{
public:
CManager(void);
CManager(int corepoolsize, int maxpoolsize, int maxjobsize, int keepalivetime);
~CManager(void);
public:
virtual int open();
virtual int svc();
virtual int close();
private:
ACE_thread_t threads[0x02];
CWorkerlist m_workerqueue;
ACE_Thread_Mutex m_mutex;
int m_corepoolsize;
int m_maxpoolsize;
int m_maxjobsize;
int m_keepalivetime;
int m_threadnumber;
bool m_done;
public:
int getcorepoolsize();
int getmaxpoolsize();
int getmaxjobsize();
void setcorepoolsize(int corepoolsize);
void setmaxpoolsize(int maxpoolsize);
void setkeepalivetime(int keepalivetime);
void setmaxjobsize(int maxjobsize);
void setthreadcount(int threadcount);
int getjobqueuesize();
void outputjobqueue();
void outputworkerqueue();
void addthreadnumber();
void reducethreadnumber();
int getthreadnumber();
public:
int submitnormaljob(const CJob* pjob, int size);
int put(CWorker* pworker);
int recyclebin(CWorker* pworker);
int getkeepalivetime();
int stop();
}; #include "Manager.h"
#include "Worker.h"
#include "Logger.h" CManager::CManager(void)
{
setcorepoolsize(5);
setmaxpoolsize(25);
setmaxjobsize(50000);
setkeepalivetime(10);
m_done = true;
m_threadnumber = 0;
} CManager::CManager(int corepoolsize = 5, int maxpoolsize = 25, int maxjobsize = 50000, int keepalivetime = 10)
{
setcorepoolsize(corepoolsize);
setmaxpoolsize(maxpoolsize);
setmaxjobsize(maxjobsize);
setkeepalivetime(keepalivetime);
m_done = true;
m_threadnumber = 0;
} CManager::~CManager(void)
{
} int CManager::open()
{
return activate(THR_NEW_LWP|THR_CANCEL_ENABLE|THR_JOINABLE, 2, 0, ACE_DEFAULT_THREAD_PRIORITY, -1, this, 0, 0, 0, threads);
} int CManager::svc()
{
if (ACE_Thread::self() == threads[0])
{
CLogger::createinstance()->loginfomsg("starts the thread of processing job and threadid=%d\n", threads[0]);
while(true)
{
ACE_Message_Block *mb_job;
getq(mb_job);
if (mb_job->msg_type() == ACE_Message_Block::MB_DATA)
{
CWorker* pworker = m_workerqueue.get();
pworker->putq(mb_job);
}
}
}
else if(ACE_Thread::self() == threads[1])
{
CLogger::createinstance()->loginfomsg("starts the thread of processing command from keyboard and threadid=%d\n", threads[2]);
while(true)
{
int command = 0;
cin>>command;
if (command == 0)
{
printf("0: help\n");
printf("1: thread info\n");
printf("2: job infomation\n");
printf("3: stop\n");
}
else if(command == 1)
{
printf("total=%d\n", getthreadnumber());
printf("corepoolsize=%d\n", getcorepoolsize());
printf("maxpoolsize=%d\n", getmaxpoolsize());
printf("keepalivetime=%d\n", getkeepalivetime());
m_workerqueue.output();
}
else if(command == 2)
{
printf("maximum=%d\n", getmaxjobsize());
printf("currentsize=%d\n", getjobqueuesize());
}
else if(command == 3)
{
stop();
ACE_OS::sleep(10);
CLogger::createinstance()->loginfomsg("the thread of processing command from keyboard and threadid=%d\n", threads[2]);
break;
}
}
}
return 0;
} int CManager::close()
{
return 0;
} int CManager::getcorepoolsize()
{
return m_corepoolsize;
} int CManager::getmaxpoolsize()
{
return m_maxpoolsize;
} int CManager::getkeepalivetime()
{
return m_keepalivetime;
} int CManager::getmaxjobsize()
{
return m_maxjobsize;
} void CManager::setcorepoolsize(int corepoolsize)
{
m_corepoolsize = corepoolsize;
} void CManager::setmaxpoolsize(int maxpoolsize)
{
m_maxpoolsize = maxpoolsize;
} void CManager::setkeepalivetime(int keepalivetime)
{
m_keepalivetime = keepalivetime;
} void CManager::setmaxjobsize(int maxjobsize)
{
m_maxjobsize = maxjobsize;
} int CManager::getjobqueuesize()
{
return msg_queue_->message_count();
} int CManager::submitnormaljob(const CJob* pjob, int size)
{
int result = -1;
if (!m_done)
{
CLogger::createinstance()->logfaultmsg("discard the job because of the threadpool has stopped to work\n");
return result;
}
if (getjobqueuesize() >= getmaxjobsize())
{
CLogger::createinstance()->logfaultmsg("discard the job because of the jobqueue is full\n");
return result;
} ACE_Message_Block *mbjob = new ACE_Message_Block(size, ACE_Message_Block::MB_DATA);
mbjob->copy((char*)pjob, size);
putq(mbjob); int idle = 0;
int busy = 0;
m_workerqueue.getsize(&idle, &busy); if (idle == 0 && getthreadnumber() < getmaxpoolsize())
{
CWorker* pworker = new CWorker(this);
addthreadnumber();
pworker->open();
put(pworker);
}
result = 0;
return result;
} int CManager::put(CWorker* pworker)
{
m_workerqueue.put(pworker);
return 0;
} int CManager::recyclebin(CWorker* pworker)
{
return m_workerqueue.recyclebin(pworker);
} void CManager::outputjobqueue()
{
ACE_Message_Block *mb;
msg_queue_->peek_dequeue_head(mb);
do
{
CJob* pjob = (CJob*)mb->base();
}
while(mb = mb->next());
} void CManager::outputworkerqueue()
{
m_workerqueue.output();
} int CManager::stop()
{
int result = -1;
m_done = false;
result = 1;
return result;
} void CManager::addthreadnumber()
{
m_mutex.acquire();
m_threadnumber ++;
m_mutex.release();
} void CManager::reducethreadnumber()
{
m_mutex.acquire();
m_threadnumber --;
m_mutex.release();
} int CManager::getthreadnumber()
{
return m_threadnumber;
}

利用ACE 自己实现的线程池的更多相关文章

  1. java 多线程 线程池:多核CPU利用ExecutorService newWorkStealingPool; ForkJoinPool线程池 执行可拆分的任务RecursiveAction;RecursiveTask

    1,给定并行级别: 1,ExecutorService newWorkStealingPool(int parallelism): 创建持有足够的线程的线程池来支持给定的并行级别,该方法还会使用多个队 ...

  2. 利用windbg 分析IIS 的线程池w3wp程序多线程挂起问题

    前几天有个朋友发个了在windows server 2008跑的IIS 跑的程序w3wp程序dmp,要我帮忙分析为何线程都挂起不运行 经过查阅资料用windbg可以调试可以输出线程的调用堆栈,但是准备 ...

  3. 利用ZoomPipeline迅速实现基于线程池的全异步TCP点对点代理

    在博文<一种基于Qt的可伸缩的全异步C/S架构服务器实现>中提到的高度模块化的类可以进行任意拆解,实现非常灵活的功能.今天,我们来看一看一个公司局域网访问英特网云服务器的点对点代理例子.代 ...

  4. C#多线程--线程池(ThreadPool)

    先引入一下线程池的概念: 百度百科:线程池是一种多线程处理形式,处理过程中将任务添加到队列,然后在创建线程后自动启动这些任务.线程池线程都是后台线程.每个线程都使用默认的堆栈大小,以默认的优先级运行, ...

  5. 【JUC】JUC线程池框架综述

    一.前言 在分析完了JUC的锁和集合框架后,下面进入JUC线程池框架的分析,下面给出JUC线程池的总体框架,之后再逐一进行分析. 二.JUC线程池框架图 说明:从上图可知,JUC线程池框架中的其他接口 ...

  6. 【多线程 5】线程池的类型以及submit()和execute()的区别

    就跟题目说的一样,本篇博客,本宝宝主要介绍两个方面的内容,其一:线程池的类型及其应用场景:其二:submit和execute的区别.那么需要再次重申的是,对于概念性的东西,我一般都是从网上挑选截取,再 ...

  7. Java线程池总结

    前一篇文章Java中实现多线程关键词整理中介绍了Java中创建多线程的各种办法,里面提到了线程池,这里对Java中的线程池做一个总结. 1. 关于ThreadPoolExecutor 为了更好地控制多 ...

  8. 这么说吧,java线程池的实现原理其实很简单

    好处 : 线程是稀缺资源,如果被无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池对线程进行统一分配.调优和监控,有以下好处: 1.降低资源消耗: 2.提高响应速度: 3.提高线 ...

  9. 【java】-- 线程池原理分析

    1.为什么要学习使用多线程? 多线程的异步执行方式,虽然能够最大限度发挥多核计算机的计算能力,但是如果不加控制,反而会对系统造成负担. 线程本身也要占用内存空间,大量的线程会占用内存资源并且可能会导致 ...

随机推荐

  1. C# - 参数数组Params

    参数数组可以使用个数不定的参数调用函数,此时用params关键字定义它们. 限制:必须是函数定义中的最后一个参数:必须是同一类型的. 优点:不用在调用代码中传入数组,指定参数个数不受限制,可以不指定该 ...

  2. 记录android学习、开发过程温故知新

    记录android学习.开发过程温故知新

  3. distinct 去重复查询——两个表join 连接,去掉重复的数据

    ------distinct 去重复查询 select * from  accounts acc join (select distinct accid from roles) r on r.acci ...

  4. 性能测试_响应时间、并发、RPS的关系

    写这篇文章是为了帮自己理清一下性能测试中最最基本,却总是被人忽略的一些概念. 并发: 什么叫并发?并发不是我们理解的在loadrunner场景中设置并发数,而是正在系统中执行操作或者在系统的队列中排队 ...

  5. [wikioi]装箱问题

    http://wikioi.com/problem/1014/ 01背包问题是最经典的动态规划之一,这道题目甚至是这其中还简单的一种,因为价值就是本身的重量了.本来比如,w是总重量限制,v[]是每个的 ...

  6. 基于FFmpeg和Qt的播放器 QtAV库

    http://blog.csdn.net/ibingow/article/details/8144795

  7. AD设计中,三种大面积覆铜的区别

    在AD设计中,主要有三种大面积覆铜方式,分别是Fill(铜皮) Polygon Pour(灌铜)和Plane(平面层),这三种方式刚开始的时候没有细细区分,现在分别应用了一下, 总结如下,欢迎指正 F ...

  8. hadoop2.2编程:各种API

    hadoop2.2 API http://hadoop.apache.org/docs/r0.23.9/api/index.html junit API http://junit.org/javado ...

  9. 转自 z55250825 的几篇关于FFT的博文(二)

    题目大意:高精度乘法.     fft的实现貌似有很多种,咱先写的是一种递归的fft,应该算是比较快的了吧.参考了 Evil君 的代码,那个运算符重载看的咱P党泪流满面. (没想到P竟然有运算符重载咩 ...

  10. 在 ASP.NET MVC 中使用 HTTPS (SSL/TLS)

    某些安全性较高的网页,如网上支付或用户登陆页面,可能会使用到https(SSL/TLS)来提高安全性.本文介绍了如何在ASP.NET MVC中强制某action使用https和如何进行向https页面 ...