VC++多线程应用--代码清单五:多线程

 
#ifndef _EXECUTETHREAD_H
#define  _EXECUTETHREAD_H

#pragma  once

#include 
" .EventDefine.h "
#include 
" .Note.h "

class  __declspec(dllexport) CEvent;

class  __declspec(dllexport) CExecuteThread
{
public:
    CExecuteThread(
void);
    
~CExecuteThread(void);
    
    
void StartExecuteThread(void);

    
void EndExecuteThread(void);
    
    
void SetEventPF(OBJECT);
    
    
void BeginRun(CNote*, CNote*, CNote*);

protected:
    
    
void Run();
    
    
void AfterRun(void);
    
    
static UINT ExecuteThreadProc(LPVOID pParam);

private:
    CWinThread
*    executethread_p;
    OBJECT      eventpf_p;
    CNote
*        tasknode_p;
    CNote
*        instancenode_p;
    CNote
*        threadnode_p;

    CEvent        threadend_e;
    CEvent        threadrun_e;
}
;

#endif /* _EXECUTETHREAD_H */

 

#include  " StdAfx.h "
#include 
" .ExecuteThread.h "
#include 
" .EventPF.h "

CExecuteThread::CExecuteThread(
void )
    : executethread_p(NULL)
    , eventpf_p(NULL)
    , tasknode_p(NULL)
    , instancenode_p(NULL)
    , threadnode_p(NULL)
{
}


CExecuteThread::
~ CExecuteThread( void )
{
    EndExecuteThread();
}


void  CExecuteThread::StartExecuteThread( void )
{
    executethread_p 
= AfxBeginThread(ExecuteThreadProc,this);
    
if (NULL == executethread_p){
        executethread_p
->m_bAutoDelete = true;
    }

}


void  CExecuteThread::EndExecuteThread( void )
{
    DWORD exitcode;
    
/*↓↓↓ add ↓↓↓*/
    
if (NULL == executethread_p){
        
return;
    }

    threadend_e.SetEvent();

    GetExitCodeThread(executethread_p
->m_hThread, &exitcode);
    
if(STILL_ACTIVE == exitcode){
        WaitForSingleObject(executethread_p
->m_hThread, INFINITE);
    }

    
/*↑↑↑ add ↑↑↑*/
}


void  CExecuteThread::SetEventPF(OBJECT evpf_p)
{
    eventpf_p 
= evpf_p;
}


void  CExecuteThread::BeginRun(CNote *  nodetask_p, CNote *  nodeobject_p, CNote *  nodethread_p)
{
    
if (NULL != nodetask_p && NULL != nodeobject_p && NULL != nodethread_p){
        
this->tasknode_p = nodetask_p;
        
this->threadnode_p = nodethread_p;
        
this->instancenode_p = nodeobject_p;
    }

    
/*Start the Event*/
    
this->threadrun_e.SetEvent();
}


void  CExecuteThread::Run( void )
{
    CMyEvent
* myEvent_p;
    CTask
*    task_p;
    CListener
* listener_p;

    task_p 
= (CTask*)tasknode_p->element_p->Get();
    listener_p 
= task_p->GetListener();
    myEvent_p 
= task_p->GetEvent();
    listener_p
->Action(NULL, myEvent_p->parameter_p);
}


void  CExecuteThread::AfterRun( void )
{
    
if (NULL != eventpf_p){
        ((CEventPF
*)eventpf_p)->AfterRunEvent(tasknode_p, instancenode_p, threadnode_p);
    }

    tasknode_p 
= NULL;
    threadnode_p 
= NULL;
    instancenode_p 
= NULL;
    
/*Start the Event*/
    
////this->threadend_e.SetEvent();
}


UINT CExecuteThread::ExecuteThreadProc(LPVOID pParam)
{
    CExecuteThread
* class_p;
    HANDLE    handles[
2];
    DWORD    wretevent;

    class_p 
= (CExecuteThread*)pParam;
    handles[
0= class_p->threadrun_e.m_hObject;
    handles[
1= class_p->threadend_e.m_hObject;
    
    
while (1){
        wretevent 
= WaitForMultipleObjects(2,handles,FALSE,100);
        
if (wretevent == WAIT_OBJECT_0){
            
/* no event */
            
break;
        }

        
else if (wretevent == (WAIT_OBJECT_0 + 1)){
            
/*do event*/
            class_p
->Run();
            class_p
->AfterRun();        
        }

    }

    
return 0;
}

POOL

#ifndef _THREADPOOL_H
#define  _THREADPOOL_H

#pragma  once

#include 
" .CircularLinkList.h "
#include 
" .ExecuteThread.h "
#include 
" .ThreadElement.h "

class  __declspec(dllexport) CThreadPool
{
public:
    CThreadPool(
void);
    
~CThreadPool(void);

    
void Initial(US, OBJECT);
    
    
bool GetIdleThread(CTokenElement**);
    
    
bool GetIdleThread(CNote**);
    
    
void IdleThreadMoveToBusyPool(CNote*);
    
    
void BusyThreadMoveToIdlePool(CNote*);
    
    
void Free(void);

protected:
    
    
void CreateIdleThread(US);

private:
    US                    threadnum;
    CCircularLinkList    idlelist;
    CCircularLinkList    busylist;
    OBJECT              eventpf_p;
    CCriticalSection    threadpool_cs;
}
;


#endif /* _THREADPOOL_H */

 

 

#include  " StdAfx.h "
#include 
" .ThreadPool.h "

CThreadPool::CThreadPool(
void )
    : threadnum(
0 )
    , eventpf_p(NULL)
{
}


CThreadPool::
~ CThreadPool( void )
{
    Free();
}


void  CThreadPool::Initial(US totalthread, OBJECT evpf_p)
{
    US i;
    
    eventpf_p 
= evpf_p;
    threadnum 
= totalthread;
    
for (i = 0; i < totalthread; i++){
        CreateIdleThread(totalthread);
    }

}


bool  CThreadPool::GetIdleThread(CTokenElement **  element_dp)
{
    CNote
*    note_p;
    
bool result = false;

    threadpool_cs.Lock();
    
if (idlelist.GetHeader(&note_p)){
        
*element_dp = note_p->element_p;
        result 
= true;
    }

    threadpool_cs.Unlock();
    
return result;
}


bool  CThreadPool::GetIdleThread(CNote **  node_dp)
{
    threadpool_cs.Lock();
    
return idlelist.GetHeader(node_dp);
    threadpool_cs.Unlock();
}


void  CThreadPool::IdleThreadMoveToBusyPool(CNote *  idlenode_p)
{
    threadpool_cs.Lock();
    idlelist.PullOut(idlenode_p);
    busylist.PushIn(idlenode_p);
    threadpool_cs.Unlock();
}


void  CThreadPool::BusyThreadMoveToIdlePool(CNote *  busynode_p)
{
    threadpool_cs.Lock();
    busylist.PullOut(busynode_p);
    idlelist.PushIn(busynode_p);
    threadpool_cs.Unlock();
}


void  CThreadPool::Free( void )
{
    CNote
*    note_p;
    threadpool_cs.Lock();
    
while (busylist.GetTail(&note_p)){
        busylist.Remove(note_p);
    }

    
while (idlelist.GetTail(&note_p)){
        idlelist.Remove(note_p);
    }

    busylist.Free();
    idlelist.Free();
    threadpool_cs.Unlock();
}


void  CThreadPool::CreateIdleThread(US totalthread)
{
    CThreadElement
*        threadElement_p;
    CExecuteThread
*        executeThread_p;

    executeThread_p 
= new CExecuteThread();
    threadElement_p 
= new CThreadElement();

    executeThread_p
->SetEventPF(eventpf_p);
    executeThread_p
->StartExecuteThread();
    threadElement_p
->Set(executeThread_p);
    idlelist.AppendTail((CTokenElement
*)threadElement_p);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值