mutex.cpp 709 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "mutex.h"
  2. namespace cmb {
  3. #if defined(_MSC_VER)
  4. mutex::mutex()
  5. {
  6. ::InitializeCriticalSection(&csection_);
  7. }
  8. mutex::~mutex()
  9. {
  10. ::DeleteCriticalSection(&csection_);
  11. }
  12. void mutex::lock()
  13. {
  14. ::EnterCriticalSection(&csection_);
  15. }
  16. void mutex::unlock()
  17. {
  18. ::LeaveCriticalSection(&csection_);
  19. }
  20. #elif defined(__GNUC__)
  21. mutex::mutex()
  22. {
  23. pthread_mutex_init(&mutx_, NULL);
  24. }
  25. mutex::~mutex()
  26. {
  27. pthread_mutex_destroy(&mutx_);
  28. }
  29. void mutex::lock()
  30. {
  31. pthread_mutex_lock(&mutx_);
  32. }
  33. void mutex::unlock()
  34. {
  35. pthread_mutex_unlock(&mutx_);
  36. }
  37. #endif
  38. }