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

#ifndef _EVENTPOOL_H
#define  _EVENTPOOL_H

#pragma  once

#include 
" .CircularLinkList.h "
#include 
" .EventElement.h "

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

    
    
void Initial(void);
    
    
void RaiseEvent(US, US, CEventPara*);
    
    
bool GetEvent(CNote**);
    
    
void CancelEvent(CNote*);
    
    
void Free(void);

protected:
    
void RaiseEvent(US, US, CEventPara*, CCircularLinkList*);

private:
    CCircularLinkList    m_LowEventList;
                     CCircularLinkList    m_MiddleEventList;
    CCircularLinkList    m_HighEventList;
    CCriticalSection    m_CriticalSection;
}
;


#endif /* _EVENTPOOL_H */

 

 

#include  " StdAfx.h "
#include 
" .EventPool.h "

CEventPool::CEventPool(
void )
{
}


CEventPool::
~ CEventPool( void )
{
    
////Free();
}


void  CEventPool::Initial( void )
{
}


void  CEventPool::RaiseEvent(US rank, US trig, CEventPara *  para_p)
{
    m_CriticalSection.Lock();
    
switch (rank){
    
case TASK_RANK_LOW:
        RaiseEvent(rank,trig,para_p,
&m_LowEventList);
        
break;
    
case TASK_RANK_MIDDLE:
        RaiseEvent(rank,trig,para_p,
&m_MiddleEventList);
        
break;
    
case TASK_RANK_HIGH:
        RaiseEvent(rank,trig,para_p,
&m_HighEventList);
        
break;
    
default:
        
break;
    }

    m_CriticalSection.Unlock();
}


bool  CEventPool::GetEvent(CNote **  node_dp)
{
    
bool result = false;

    m_CriticalSection.Lock();
    result 
= m_HighEventList.GetHeader(node_dp);
    
if (!result){
        result 
= m_MiddleEventList.GetHeader(node_dp);
        
if (!result){
            result 
= m_LowEventList.GetHeader(node_dp);
        }

    }

    
return result;
    m_CriticalSection.Lock();
}



void  CEventPool::CancelEvent(CNote *  node_p)
{
    m_CriticalSection.Lock();
    m_HighEventList.Remove(node_p);
    m_MiddleEventList.Remove(node_p);
    m_LowEventList.Remove(node_p);
    m_CriticalSection.Lock();
}


void  CEventPool::Free( void )
{
    m_CriticalSection.Lock();
    m_HighEventList.Free();
    m_MiddleEventList.Free();
    m_LowEventList.Free();
    m_CriticalSection.Lock();
}



void  CEventPool::RaiseEvent(US rank, US trig, CEventPara *  para_p, CCircularLinkList *  list_p)
{
    CEventElement
* eventElement_p;
    CMyEvent
*    myEvent_p;

    myEvent_p 
= new CMyEvent();
    eventElement_p 
= new CEventElement();
    myEvent_p
->m_Trigger = trig;
    myEvent_p
->m_Priority = rank;
    myEvent_p
->parameter_p->Copy(&para_p);
    eventElement_p
->Set(myEvent_p);
    list_p
->AppendTail(eventElement_p);
}

 

 

#ifndef _LISTENERPOOL_H
#define  _LISTENERPOOL_H

#pragma  once

#include 
" .CircularLinkList.h "
#include 
" .HashTable.h "
#include 
" .ListElement.h "
#include 
" .ListenerElement.h "

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

    
void Initial(void);

    
void RaiseListener(CListener*);
    
    
void CancelListener(US);
    
void CancelListener(CListener*);
    
    
bool FetchTrigListener(US, CListElement**);
    
    
bool FetchTrigListener(US, CNote**);

    
void RaiseRunningInstance(OBJECT, CNote**);
    
    
void CancelRunningInstance(OBJECT);
    
    
void CancelRunningInstance(CNote*);
    
    
bool IsRunningInstance(OBJECT);
    
    
void Free(void);

private:
    CHashTable            m_ListenerListTable;
    CCircularLinkList    m_InstanceList;
    CCriticalSection    m_CriticalSection;
}
;


#endif /* _LISTENERPOOL_H */

 

 

#include  " StdAfx.h "
#include 
" .ListenerPool.h "

CListenerPool::CListenerPool(
void )
{
}


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


void  CListenerPool::Initial( void )
{
}


void  CListenerPool::RaiseListener(CListener *  listener_p)
{
    US trig;
    CListenerElement
* listenerElement_p;
    CListElement
*        listElement_p;
    CCircularLinkList
*    circularLinkList_p;
    CNote
*    note_p;

    m_CriticalSection.Lock();
    
if (NULL != listener_p){
        listenerElement_p 
= new CListenerElement();
        trig 
= listener_p->GetTrig();

        listenerElement_p
->Set(listener_p);
        
if (m_ListenerListTable.Search(trig,&note_p)){
            
/*finded listener list*/
            listElement_p 
= (CListElement*)note_p->element_p->Get();
            circularLinkList_p 
= listElement_p->Get();
            circularLinkList_p
->AppendTail((CTokenElement*)listenerElement_p);
        }

        
else{
            
/*not find*/
            circularLinkList_p 
= new CCircularLinkList();
            listElement_p 
= new CListElement();
            circularLinkList_p
->AppendTail((CTokenElement*)listenerElement_p);
            listElement_p
->Set(circularLinkList_p);
            
this->m_ListenerListTable.Raise(trig,(CTokenElement*)listElement_p);
        }

    }

    m_CriticalSection.Unlock();
}


void  CListenerPool::CancelListener(US trig)
{
    m_CriticalSection.Lock();
    m_ListenerListTable.Remove(trig);
    m_CriticalSection.Unlock();
}


void  CListenerPool::CancelListener(CListener *  listener_p)
{
    US trig;
    CCircularLinkList
* listenerlist_p;
    CNote
*  note_p;

    m_CriticalSection.Lock();
    
if (NULL != listener_p){
        
/*↓↓↓↓↓↓↓↓↓*/
        trig 
= listener_p->GetTrig();
        
/*↑↑↑↑↑↑↑↑↑*/
        
if (m_ListenerListTable.Search(trig,&note_p)){
            
/*Get listener list*/
            listenerlist_p 
= (CCircularLinkList*)(note_p->element_p->Get());
            
/*delete listener In Link List*/
            listenerlist_p
->Remove((OBJECT)listener_p);
        }

    }

    m_CriticalSection.Unlock();
}


bool  CListenerPool::FetchTrigListener(US trig, CListElement **  listener_dp)
{
    
bool result = false;

    m_CriticalSection.Lock();
    
if (m_ListenerListTable.Search(trig,(CTokenElement**)listener_dp)){
        result 
= true;
    }

    m_CriticalSection.Unlock();

    
return result;
}


bool  CListenerPool::FetchTrigListener(US trig, CNote **  node_dp)
{
    
bool result = false;

    m_CriticalSection.Lock();
    
if (m_ListenerListTable.Search(trig,node_dp)){
        result 
= true;
    }

    m_CriticalSection.Unlock();
    
return result;
}


void  CListenerPool::RaiseRunningInstance(OBJECT instance_p, CNote **  node_dp)
{
    CTokenElement
*    tokenElement_p;

    m_CriticalSection.Lock();
    tokenElement_p 
= new CTokenElement();
    tokenElement_p
->Set(instance_p);
    m_InstanceList.AppendTail(tokenElement_p);
    m_InstanceList.GetTail(node_dp);
    m_CriticalSection.Unlock();
}


void  CListenerPool::CancelRunningInstance(OBJECT instance_p)
{
    m_CriticalSection.Lock();
    m_InstanceList.Remove(instance_p);
    m_CriticalSection.Unlock();
}


void  CListenerPool::CancelRunningInstance(CNote *  node_p)
{
    m_CriticalSection.Lock();
    m_InstanceList.Remove(node_p);
    m_CriticalSection.Unlock();
}


bool  CListenerPool::IsRunningInstance(OBJECT instance_p)
{
    CNote
*    note_p;
    
bool    result = false;

    m_CriticalSection.Lock();
    
if (m_InstanceList.GetHeader(&note_p)){
        
while (    NULL != note_p){
            
/*Check Instance*/
            
if (instance_p == note_p->element_p->Get()){
                result 
= true;
                
break;
            }

            
/*Next Node*/
            m_InstanceList.GetNext(note_p);
        }

    }

    m_CriticalSection.Unlock();
    
return result;
}


void  CListenerPool::Free( void )
{
    CNote
*    node_p;
    m_CriticalSection.Lock();
    
while (NULL != m_ListenerListTable.SearchAllNode(&node_p)){
        
////delete node_p->element_p;
        
////delete node_p;

        m_ListenerListTable.Remove(node_p->key);
    }

    m_ListenerListTable.Free();
    m_InstanceList.Free();
    m_CriticalSection.Unlock();
}

哈希表

 

#ifndef _HASHTABLE_H
#define  _HASHTABLE_H

#pragma  once

#include 
" .OrdinalLinkList.h "

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

    
void Raise(US, CTokenElement*);

    
void Remove(US);

    
bool Search(US, CNote**);
    
bool Search(US, CTokenElement**);

    
bool SearchAllNode(CNote**);

    
void Free(void);

protected:
    US HashFunc(US);

private:
    COrdinalLinkList hashtable_t[HASHSIZE];
}
;


#endif /* _HASHTABLE_H */

 

 

#include  " StdAfx.h "
#include 
" .HashTable.h "

CHashTable::CHashTable(
void )
{
}


CHashTable::
~ CHashTable( void )
{
////    Free();
}


void  CHashTable::Raise(US key, CTokenElement *  element_p)
{
    hashtable_t[HashFunc(key)].Insert(key,element_p);
}


void  CHashTable::Remove(US key)
{
    hashtable_t[HashFunc(key)].Remove(key);
}


bool  CHashTable::Search(US key, CNote **  node_dp)
{
    
bool result = false;
    CNote
* note_p;

    
if(hashtable_t[HashFunc(key)].Search(key,&note_p)){
        
*node_dp = note_p;
        result 
= true;
    }

    
return result;
}


bool  CHashTable::Search(US key, CTokenElement **  element_dp)
{
    
bool result = false;
    CNote
* note_p;

    
if(hashtable_t[HashFunc(key)].Search(key,&note_p)){
        
*element_dp = note_p->element_p;
        result 
= true;
    }

    
return result;
}


bool  CHashTable::SearchAllNode(CNote **  node_dp)
{
    
int i;
    
bool result = false;

    
for (i = 0; i < HASHSIZE; i++){
        result 
= hashtable_t[i].GetHeader(node_dp);
        
if (result){
            
break;/*find the node break this method*/
        }

    }

    
return result;
}


void  CHashTable::Free( void )
{
    
int i;

    
for (i = 0; i<HASHSIZE; i++){
        hashtable_t[i].Free();
    }

}


US CHashTable::HashFunc(US key)
{
    
return (key % HASHSIZE);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

进击的横打

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

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

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

打赏作者

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

抵扣说明:

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

余额充值