Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1020 B

  1. #ifndef THREADLOCK_H
  2. #define THREADLOCK_H
  3. class CThreadLock
  4. {
  5. public:
  6. CThreadLock()
  7. {
  8. InitializeCriticalSection(&_csAccess);
  9. }
  10. ~CThreadLock()
  11. {
  12. DeleteCriticalSection(&_csAccess);
  13. }
  14. void ThreadLock()
  15. {
  16. EnterCriticalSection(&_csAccess);
  17. }
  18. void ThreadUnLock()
  19. {
  20. LeaveCriticalSection(&_csAccess);
  21. }
  22. private:
  23. CRITICAL_SECTION _csAccess; // Used to control access to member data
  24. // Do not allow this object to be copied.
  25. CThreadLock(const CThreadLock &that);
  26. operator=(const CThreadLock &that);
  27. };
  28. class CAutoLock
  29. {
  30. public:
  31. CAutoLock(CThreadLock *pThis) : _pThis(pThis)
  32. {
  33. _pThis->ThreadLock();
  34. }
  35. ~CAutoLock()
  36. {
  37. _pThis->ThreadUnLock();
  38. }
  39. private:
  40. CThreadLock *_pThis;
  41. };
  42. //
  43. //
  44. // LOCK_LOCALS() should be used whenever access to thread-safe member data
  45. // is needed.
  46. //
  47. #define LOCK_LOCALS(pObj) CAutoLock local_lock(pObj);
  48. #endif