互斥对象锁和临界区锁性能比较


在Win32平台上进行多线程编程,常会用到锁。下边用C++实现了互斥对象(Mutex)锁和临界区(CRITICAL_SECTION)锁,以加深理解和今后方便使用。代码已在VS2005环境下编译测试通过。

Lock.h

  1. #ifndef _Lock_H   
  2. #define _Lock_H   
  3.   
  4. #include <windows.h>   
  5.   
  6.   
  7. //锁接口类   
  8. class ILock  
  9. {  
  10. public:  
  11.     virtual ~ILock() {}  
  12.   
  13.     virtual void Lock() const = 0;  
  14.     virtual void Unlock() const = 0;  
  15. };  
  16.   
  17. //互斥对象锁类   
  18. class Mutex : public ILock  
  19. {  
  20. public:  
  21.     Mutex();  
  22.     ~Mutex();  
  23.   
  24.     virtual void Lock() const;  
  25.     virtual void Unlock() const;  
  26.   
  27. private:  
  28.     HANDLE m_mutex;  
  29. };  
  30.   
  31. //临界区锁类   
  32. class CriSection : public ILock  
  33. {  
  34. public:  
  35.     CriSection();  
  36.     ~CriSection();  
  37.   
  38.     virtual void Lock() const;  
  39.     virtual void Unlock() const;  
  40.   
  41. private:  
  42.     CRITICAL_SECTION m_critclSection;  
  43. };  
  44.   
  45.   
  46. //锁   
  47. class CMyLock  
  48. {  
  49. public:  
  50.     CMyLock(const ILock&);  
  51.     ~CMyLock();  
  52.   
  53. private:  
  54.     const ILock& m_lock;  
  55. };  
  56.   
  57.   
  58. #endif  

Lock.cpp

  1. #include "Lock.h"   
  2.   
  3. //---------------------------------------------------------------------------   
  4.   
  5. //创建一个匿名互斥对象   
  6. Mutex::Mutex()  
  7. {  
  8.     m_mutex = ::CreateMutex(NULL, FALSE, NULL);  
  9. }  
  10.   
  11. //销毁互斥对象,释放资源   
  12. Mutex::~Mutex()  
  13. {  
  14.     ::CloseHandle(m_mutex);  
  15. }  
  16.   
  17. //确保拥有互斥对象的线程对被保护资源的独自访问   
  18. void Mutex::Lock() const  
  19. {  
  20.     DWORD d = WaitForSingleObject(m_mutex, INFINITE);  
  21. }  
  22.   
  23. //释放当前线程拥有的互斥对象,以使其它线程可以拥有互斥对象,对被保护资源进行访问   
  24. void Mutex::Unlock() const  
  25. {  
  26.     ::ReleaseMutex(m_mutex);  
  27. }  
  28.   
  29. //---------------------------------------------------------------------------   
  30.   
  31. //初始化临界资源对象   
  32. CriSection::CriSection()  
  33. {  
  34.     ::InitializeCriticalSection(&m_critclSection);  
  35. }  
  36.   
  37. //释放临界资源对象   
  38. CriSection::~CriSection()  
  39. {  
  40.     ::DeleteCriticalSection(&m_critclSection);  
  41. }  
  42.   
  43. //进入临界区,加锁   
  44. void CriSection::Lock() const  
  45. {  
  46.     ::EnterCriticalSection((LPCRITICAL_SECTION)&m_critclSection);  
  47. }     
  48.   
  49. //离开临界区,解锁   
  50. void CriSection::Unlock() const  
  51. {  
  52.     ::LeaveCriticalSection((LPCRITICAL_SECTION)&m_critclSection);  
  53. }  
  54.   
  55. //---------------------------------------------------------------------------   
  56.   
  57. //利用C++特性,进行自动加锁   
  58. CMyLock::CMyLock(const ILock& m) : m_lock(m)  
  59. {  
  60.     m_lock.Lock();  
  61. }  
  62.   
  63. //利用C++特性,进行自动解锁   
  64. CMyLock::~CMyLock()  
  65. {  
  66.     m_lock.Unlock();  
  67. }  
  • 1
  • 2
  • 下一页

相关内容