linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > article > Linux开发区 > 软件开发 >
栏目导栏
资料搜索
热门文章
·Linux 下 C 语言编程
·Linux下的通用线程池创建
·C++字符串转换篇
·linux C 进程操作篇
·linux上的C/C++编译器gcc/egcs
·linux C 文件权限控制篇
·GCC使用手册
·linux C 接口处理篇
·在Redhat Linux上安装 GCC 编译
·fopen()函数的参数说明
·C语言运算符
·GCC使用指南
·Linux下C开发环境的构成和安装
·GCC使用手册与常用命令
·Linux常用C函数-日期时间篇
最新文章
·epoll入门
·在Linux下发布程序需要注意版本
·Suse Linux系统下JAVA AWT界面
·Vim编译器配合ctags实现函数原
·在Ubuntu Linux 8.04上构建GCC
·Linux操作系统下Socket编程地址
·将VC程序移植到Linux系统的几点
·Linux下malloc/free与new/dele
·Linux下用GTK和socket实现简单
·Linux操作系统下让Tomcat启动在
·Linux操作系统中如何编译C程序
·几种常被人们忽略的Linux系统下
·Eclipse编程工具 在Ubuntu下的
·Linux操作系统下的网络地址转换
·老手经验谈:Linux驱动程序开发
Google
 
Linux下的通用线程池创建
[ 作者:  加入时间:2005-11-25 10:29:41  来自: ]
push_back(jobthread);
    m_ThreadList.push_back(jobthread);
    m_IdleMutex.Unlock();
}
 
//move and idle thread to busy thread
void CThreadPool::MoveToBusyList(CWorkerThread* idlethread)
{
    m_BusyMutex.Lock();
    m_BusyList.push_back(idlethread);
    m_AvailNum--;
    m_BusyMutex.Unlock();
  
    m_IdleMutex.Lock();
    vector<CWorkerThread*>::iterator pos;
    pos = find(m_IdleList.begin(),m_IdleList.end(),idlethread);
    if(pos !=m_IdleList.end())
    m_IdleList.erase(pos);
    m_IdleMutex.Unlock();
}
 
void CThreadPool::MoveToIdleList(CWorkerThread* busythread)
{
    m_IdleMutex.Lock();
    m_IdleList.push_back(busythread);
    m_AvailNum++;
    m_IdleMutex.Unlock();
 
    m_BusyMutex.Lock();
    vector<CWorkerThread*>::iterator pos;
    pos = find(m_BusyList.begin(),m_BusyList.end(),busythread);
    if(pos!=m_BusyList.end())
    m_BusyList.erase(pos);
    m_BusyMutex.Unlock();
 
    m_IdleCond.Signal();
    m_MaxNumCond.Signal();
}
 
//create num idle thread and put them to idlelist
void CThreadPool::CreateIdleThread(int num)
{
    for(int i=0;i<num;i++){
    CWorkerThread* thr = new CWorkerThread();
    thr->SetThreadPool(this);
    AppendToIdleList(thr);
    m_VarMutex.Lock();
    m_AvailNum++;
    m_VarMutex.Unlock();
    thr->Start();       //begin the thread,the thread wait for job
    }
}
 
void CThreadPool::DeleteIdleThread(int num)
{
    printf("Enter into CThreadPool::DeleteIdleThreadn");
    m_IdleMutex.Lock();
    printf("Delete Num is %dn",num);
    for(int i=0;i<num;i++){
    CWorkerThread* thr;
    if(m_IdleList.size() > 0 ){
            thr = (CWorkerThread*)m_IdleList.front();
            printf("Get Idle thread %dn",thr->GetThreadID());
    }
 
    vector<CWorkerThread*>::iterator pos;
    pos = find(m_IdleList.begin(),m_IdleList.end(),thr);
    if(pos!=m_IdleList.end())
        m_IdleList.erase(pos);
    m_AvailNum--;
    printf("The idle thread available num:%d n",m_AvailNum);
    printf("The idlelist              num:%d n",m_IdleList.size());
    }
    m_IdleMutex.Unlock();
}
void CThreadPool::Run(CJob* job,void* jobdata)
{
    assert(job!=NULL);
   
    //if the busy thread num adds to m_MaxNum,so we should wait
    if(GetBusyNum() == 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)
    {
    idlethr->m_WorkMutex.Lock();
    MoveToBusyList(idlethr);
    idlethr->SetThreadPool(this);
    job->SetWorkThread(idlethr);
   Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
分页:1 2 3 [4] 5 6 7
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息