#include "stdio.h"
#include "assert.h"
#include "stdlib.h"
#include <algorithm>
#include "ThreadPool.h"
#include "WorkerThread.h"
#include "Job.h"
CThreadPool::CThreadPool():m_shutDown(FALSE)
{
m_maxNum = CThreadPool::THREAD_MAX_NUM;
m_availLow = CThreadPool::THREAD_AVAIL_LOW;
m_initNum = m_availNum = CThreadPool::THREAD_INIT_NUM;
m_availHigh = CThreadPool::THREAD_AVAIL_HIGH;
//初始化线程数目不超过最大线程数目
assert(m_initNum > 0 && m_initNum <= m_maxNum);
//保证初始化的线程数目在最小和最大的空闲线程数目之间则合理
assert(m_initNum >= m_availLow && m_initNum <= m_availHigh);
m_threadList.clear();
m_busyList.clear();
m_idleList.clear();
for (unsigned int i = 0; i < m_initNum; ++i)
{
CWorkerThread* thr = new CWorkerThread();
assert(thr);
thr->SetThreadPool(this);
AppendToIdleList(thr);
thr->Start();
}
}
CThreadPool::CThreadPool(int initNum):m_shutDown(FALSE)
{
m_maxNum = CThreadPool::THREAD_MAX_NUM;
m_availLow = CThreadPool::THREAD_AVAIL_LOW;
m_initNum = m_availNum = initNum;
m_availHigh = CThreadPool::THREAD_AVAIL_HIGH;
//初始化线程数目不超过最大线程数目
assert(initNum > 0 && (unsigned int)initNum <= m_maxNum);
//保证初始化的线程数目在最小和最大的空闲线程数目之间则合理
assert((unsigned int)initNum >= m_availLow && (unsigned int)initNum <= m_availHigh);
m_threadList.clear();
m_busyList.clear();
m_idleList.clear();
for (unsigned int i = 0; i < m_initNum; ++i)
{
CWorkerThread* thr = new CWorkerThread();
assert(thr);
thr->SetThreadPool(this);
AppendToIdleList(thr);
thr->Start();
}
}
CThreadPool::~CThreadPool()
{
TerminateAll();
}
void CThreadPool::TerminateAll()
{
m_shutDown = TRUE;
for (unsigned int i = 0; i < m_threadList.size(); ++i)
{
CWorkerThread* thr = m_threadList[i];
if (thr) thr->Terminate();
thr = NULL;
}
return;
}
CWorkerThread* CThreadPool::GetIdleThread(void)
{
if (m_shutDown) return NULL;
while (m_idleList.size() == 0)
{
printf("no idle thread, wait pls...\n");
m_idleCond.Wait();
}
m_idleMutex.Lock();
if (m_idleList.size() > 0)
{
CWorkerThread* thr = (CWorkerThread*)m_idleList.front();
if (!thr) return NULL;
//printf("get idle thread [%d]\n", thr->GetThreadID());
m_idleMutex.Unlock();
return thr;
}
m_idleMutex.Unlock();
return NULL;
}
void CThreadPool::AppendToIdleList(CWorkerThread* jobThread)
{
if (m_shutDown) return;
if (!jobThread) return;
m_idleMutex.Lock();
m_idleList.push_back(jobThread);
m_threadList.push_back(jobThread);
m_idleMutex.Unlock();
}
void CThreadPool::MoveToBusyList(CWorkerThread* idleThread)
{
if (m_shutDown) return;
if (!idleThread) return;
m_busyMutex.Lock();
m_busyList.push_back(idleThread);
m_availNum --;
m_busyMutex.Unlock();
m_idleMutex.Lock();
vector<CWorkerThread*>::iterator iter;
iter = find(m_idleList.begin(), m_idleList.end(), idleThread);
if (iter != m_idleList.end())
m_idleList.erase(iter);
m_idleMutex.Unlock();
}
void CThreadPool::MoveToIdleList(CWorkerThread* busyThread)
{
if (m_shutDown) return;
if (!busyThread) return;
m_idleMutex.Lock();
m_idleList.push_back(busyThread);
m_availNum ++;
m_idleMutex.Unlock();
m_busyMutex.Lock();
vector<CWorkerThread*>::iterator iter;
iter = find(m_busyList.begin(), m_busyList.end(), busyThread);
if (iter != m_busyList.end())
m_busyList.erase(iter);
m_busyMutex.Unlock();
m_idleCond.Signal();
m_maxNumCond.Signal();
}
void CThreadPool::CreateIdleThread(int num)
{
if (m_shutDown) return;
if (num <= 0) return;
//printf("add new %d threads\n", num);
for (int i = 0; i < num; ++i)
{
CWorkerThread* thr = new CWorkerThread();
if (!thr) continue;
thr->SetThreadPool(this);
AppendToIdleList(thr);
m_varMutex.Lock();
m_availNum ++;
m_varMutex.Unlock();
thr->Start();
}
}
void CThreadPool::DeleteIdleThread(int num)
{
if (m_shutDown) return;
if (num <= 0) return;
m_idleMutex.Lock();
//如果空闲线程数目小于允许最大空闲线程数目,则不再删除
if (m_idleList.size() < m_availHigh)
{
//printf("idle thread list size=%d\n", m_idleList.size());
m_idleMutex.Unlock();
return;
}
//printf("delete %d threads\n", num);
for (int i = 0; i < num; ++i)
{
CWorkerThread* thr = NULL;
if (m_idleList.size() > 0)
thr = (CWorkerThread*)m_idleList.front();
if (!thr) continue;
if (THREAD_IDLE != thr->GetThreadState()) continue;
vector<CWorkerThread*>::iterator iter;
iter = find(m_idleList.begin(), m_idleList.end(), thr);
if (iter != m_idleList.end())
{
//printf("delete thread [%d]\n", (*iter)->GetThreadID());
DestroyIdleThread(*iter);
m_idleList.erase(iter);
}
m_availNum --;
}
//printf("after delete, idle threads num:%d, idle list size=%d\n", m_availNum, m_idleList.size());
m_idleMutex.Unlock();
}
void CThreadPool::DestroyIdleThread(CWorkerThread* idleThread)
{
if (m_shutDown) return;
if (!idleThread) return;
if (THREAD_IDLE != idleThread->GetThreadState()) return;
vector<CWorkerThread*>::iterator iter;
iter = find(m_threadList.begin(), m_threadList.end(), idleThread);
if (iter != m_threadList.end())
{
CWorkerThread *delThread = *iter;
if (!delThread) return;
delThread->Terminate();
m_threadList.erase(iter);
}
}
void CThreadPool::Run(CJob* job, void* JobData)
{
if (m_shutDown) return;
assert(job != NULL);
//线程池已经达到最大并发数目,则等待
if (GetBusyNum() >= m_maxNum)
{
printf("busy threads reach max permit num:%d\n", m_maxNum);
m_maxNumCond.Wait();
}
if (m_idleList.size() < m_availLow)
{
if (GetAllNum() + m_initNum - m_idleList.size() < m_maxNum)
CreateIdleThread(m_initNum - m_idleList.size());
else
CreateIdleThread(m_maxNum - GetAllNum());
}
CWorkerThread* idleThr = GetIdleThread();
if (idleThr != NULL)
{
MoveToBusyList(idleThr);
idleThr->SetThreadPool(this);
job->SetWorkThread(idleThr);
idleThr->SetJob(job, JobData);
//printf("job [%d] is bind to thread [%d]\n", job->GetJobNo(), idleThr->GetThreadID());
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
thread_pool.rar (82个子文件)
thread_pool
thread_pool.sdf 28.83MB
Debug
thread_pool.ilk 719KB
thread_pool.exe 84KB
thread_pool.pdb 1019KB
ipch
thread_pool-2c5a8fa1
thread_pool-63586bd.ipch 43.31MB
thread_pool.sln 900B
thread_pool.suo 39KB
thread_pool
ThreadPool.cpp 6KB
ThreadCondition.cpp 417B
test.cpp 849B
thread_pool.vcxproj.filters 2KB
Debug
cl.command.1.tlog 5KB
link.6100-cvtres.read.1.tlog 2B
link.2132.write.1.tlog 2B
rc.command.1.tlog 512B
CL.read.1.tlog 101KB
link.5060.write.1.tlog 2B
vc100.idb 691KB
mt.read.1.tlog 278B
link.5364-cvtres.read.1.tlog 2B
link.5364-cvtres.write.1.tlog 2B
link.5364.write.1.tlog 2B
link-cvtres.read.1.tlog 2B
link.read.1.tlog 4KB
Thread.obj 29KB
thread_pool.lastbuildstate 48B
link.5812-cvtres.read.1.tlog 2B
link.6100-cvtres.write.1.tlog 2B
test.obj 69KB
link.5812-cvtres.write.1.tlog 2B
rc.read.1.tlog 250B
ThreadManage.obj 41KB
link.write.1.tlog 1KB
link.4684-cvtres.read.1.tlog 2B
CL.write.1.tlog 8KB
vc100.pdb 324KB
link.2132.read.1.tlog 2B
link.6100.write.1.tlog 2B
thread_pool.vcxprojResolveAssemblyReference.cache 713B
thread_pool.exe.embed.manifest 406B
thread_pool.exe.intermediate.manifest 381B
link.4684.read.1.tlog 2B
link.5060-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
thread_pool.write.1.tlog 0B
ThreadMutex.obj 24KB
link.5364.read.1.tlog 2B
link.5060.read.1.tlog 2B
link.6100.read.1.tlog 2B
Job.obj 8KB
link.5812.read.1.tlog 2B
link.4684-cvtres.write.1.tlog 2B
mt.write.1.tlog 278B
ThreadCondition.obj 23KB
thread_pool.Build.CppClean.log 2KB
link.2132-cvtres.write.1.tlog 2B
thread_pool.exe.embed.manifest.res 472B
thread_pool_manifest.rc 212B
link.5812.write.1.tlog 2B
link.5060-cvtres.read.1.tlog 2B
link.2132-cvtres.read.1.tlog 2B
ThreadPool.obj 215KB
mt.command.1.tlog 380B
link.4684.write.1.tlog 2B
rc.write.1.tlog 258B
link-cvtres.write.1.tlog 2B
WorkerThread.obj 48KB
thread_pool.log 2KB
thread_pool.vcxproj.user 143B
Thread.cpp 1KB
ThreadMutex.cpp 460B
Job.h 511B
ThreadMutex.h 243B
ThreadManage.cpp 594B
thread_pool.vcxproj 4KB
Job.cpp 515B
Thread.h 991B
ThreadPool.h 2KB
ThreadCondition.h 265B
ThreadManage.h 404B
WorkerThread.cpp 2KB
WorkerThread.h 611B
共 82 条
- 1
资源评论
_July_12
- 粉丝: 8
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Python语言教程1-python批量图片重命名,将后缀某几个不想要的字去除
- Space Combat Kit 太空战斗套件Unity游戏开发插件资源unitypackage C#
- Universal Device Preview 通用设备预览Unity游戏开发插件资源unitypackage
- Paladin Anim Set 圣骑士动画集Unity游戏动作动画插件资源unitypackage
- 计算机财务管理期末考报表部分题目及答案.doc
- 计算机软件维护论文.doc
- 计算机软件著作权授权书.docx
- 计算机键盘教学案例.doc
- 计算流体力学软件平台方案.pptx
- 计量自动化系统介绍.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功