Kisser Leon

这个kisser不太冷
posts - 100, comments - 102, trackbacks - 0, articles - 0

创建自己的CriticalSection和Lock

Posted on 2007-04-04 21:38 kk 阅读(848) 评论(0)  编辑 收藏 引用 所属分类: IT

Lockable.h: CriticalSection的抽象父类

class LockableObject

{

      public:

            LockableObject(){}

            virtual ~LockableObject(){}

            virtual void Lock() = 0;

            virtual void Unlock() = 0;

};

 

CriticalSection.h

#include "LockableObject.h"

#include <windows.h>

 

class CriticalSection : public LockableObject

{

      public:

            CriticalSection();

            virtual ~CriticalSection();

            virtual void Lock();

            virtual void Unlock();

      private:

            CRITICAL_SECTION mCriticalSection;

};

 

CriticalSection.cpp

#include "CriticalSection.h"

 

CriticalSection::CriticalSection()

{

      InitializeCriticalSection(&mCriticalSection);

}

 

CriticalSection::~CriticalSection()

{

      DeleteCriticalSection(&mCriticalSection);

}

 

void CriticalSection::Lock()

{

      EnterCriticalSection(&mCriticalSection);

}

 

void CriticalSection::Unlock()

{

      LeaveCriticalSection(&mCriticalSection);

}

 

Lock.h

#include "CriticalSection.h"

 

class Lock

{

      public:

            Lock(LockableObject* pLockable);

            ~Lock();

      private:

            LockableObject* mLockable;

};

 

Lock.cpp

#include "Lock.h"

 

Lock::Lock(LockableObject* pLockable)

{

      mLockable = pLockable;

      pLockable->Lock();

}

 

Lock::~Lock()

{

      mLockable->Unlock();

}

     

Google

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理