Mutex.cpp 656 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "Mutex.h"
  2. Mutex::Mutex()
  3. {
  4. #if defined(WIN32)
  5. m_mutex = ::CreateMutex(NULL, FALSE, NULL);
  6. #else
  7. pthread_mutex_init(&mutex, NULL);
  8. #endif
  9. }
  10. Mutex::~Mutex()
  11. {
  12. #if defined(WIN32)
  13. ::CloseHandle(m_mutex);
  14. #else
  15. pthread_mutex_destroy(&mutex);
  16. #endif
  17. }
  18. int Mutex::Lock() const
  19. {
  20. #if defined(WIN32)
  21. DWORD ret = WaitForSingleObject(m_mutex, INFINITE);
  22. #else
  23. int ret = pthread_mutex_lock((pthread_mutex_t*)&mutex);
  24. #endif
  25. return ret;
  26. }
  27. int Mutex::Unlock() const
  28. {
  29. #if defined(WIN32)
  30. bool ret = ::ReleaseMutex(m_mutex);
  31. #else
  32. int ret = pthread_mutex_unlock((pthread_mutex_t*)&mutex);
  33. #endif
  34. return ret;
  35. }