LockMRSW.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:1k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // LockMRSW.h: interface for the CLockMRSW & CLockMRSW2 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_LOCKMRSW_H__F65AF80D_9748_4100_B0AC_823695855E5D__INCLUDED_)
  5. #define AFX_LOCKMRSW_H__F65AF80D_9748_4100_B0AC_823695855E5D__INCLUDED_
  6. //lock: multi read & single write
  7. //reading is parallel, and writing is exclusive
  8. class CLockMRSW 
  9. {
  10. public:
  11. CLockMRSW();
  12. virtual ~CLockMRSW();
  13. public:
  14. void EnterRead();
  15. void LeaveRead();
  16. void EnterWrite();
  17. void LeaveWrite();
  18. private:
  19. CRITICAL_SECTION m_csExclusive;
  20. CRITICAL_SECTION m_csModi;
  21. HANDLE m_eventIdle;
  22. LONG m_lUsing;
  23. };
  24. //assistant
  25. struct KAutoLockRead
  26. {
  27. CLockMRSW& theLock;
  28. KAutoLockRead(CLockMRSW& locksrc) : theLock(locksrc) {theLock.EnterRead();}
  29. ~KAutoLockRead() {theLock.LeaveRead();}
  30. };
  31. struct KAutoLockWrite
  32. {
  33. CLockMRSW& theLock;
  34. KAutoLockWrite(CLockMRSW& locksrc) : theLock(locksrc) {theLock.EnterWrite();}
  35. ~KAutoLockWrite() {theLock.LeaveWrite();}
  36. };
  37. //for more easy
  38. #define AUTOLOCKREAD(lock)
  39. KAutoLockRead __local_autolockread_(lock);
  40. #define AUTOLOCKREAD_(name, lock)
  41. KAutoLockRead name(lock);
  42. #define AUTOLOCKWRITE(lock)
  43. KAutoLockWrite __local_autolockwrite_(lock);
  44. #define AUTOLOCKWRITE_(name, lock)
  45. KAutoLockWrite name(lock);
  46. #endif // !defined(AFX_LOCKMRSW_H__F65AF80D_9748_4100_B0AC_823695855E5D__INCLUDED_)