cond_variable.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // C++ wrapper for pthread_cond_t
  2. //
  3. // Platform: ISO C++ 98/11 - POSIX
  4. // $Id$
  5. //
  6. // (c) __vic 2007
  7. #ifndef __VIC_POSIX_COND_VARIABLE_H
  8. #define __VIC_POSIX_COND_VARIABLE_H
  9. #include<__vic/posix/_cfg.h>
  10. #include<__vic/defs.h>
  11. #include<__vic/posix/mutex.h>
  12. #include<pthread.h>
  13. namespace __vic { namespace posix {
  14. //////////////////////////////////////////////////////////////////////////////
  15. // Condition variable
  16. class cond_variable
  17. {
  18. ::pthread_cond_t cond
  19. #if __cplusplus >= 201103L
  20. = PTHREAD_COND_INITIALIZER
  21. #endif
  22. ;
  23. #if __cplusplus < 201103L
  24. cond_variable(const cond_variable & ); // not implemented
  25. cond_variable &operator=(const cond_variable & ); // not implemented
  26. #else
  27. public:
  28. cond_variable(const cond_variable & ) = delete;
  29. cond_variable &operator=(const cond_variable & ) = delete;
  30. #endif
  31. public:
  32. #if __cplusplus >= 201103L
  33. constexpr cond_variable() noexcept = default;
  34. #else
  35. cond_variable() { ::pthread_cond_init(&cond, nullptr); }
  36. #endif
  37. ~cond_variable() { ::pthread_cond_destroy(&cond); }
  38. void wait(::pthread_mutex_t & );
  39. void wait(mutex &m) { wait(*m.handle()); }
  40. bool wait_until(::pthread_mutex_t & , const ::timespec & );
  41. bool wait_until(mutex &m, const ::timespec &abstime)
  42. { return wait_until(*m.handle(), abstime); }
  43. bool wait_for(::pthread_mutex_t & , unsigned long ); // msec
  44. bool wait_for(mutex &m, unsigned long msec)
  45. { return wait_for(*m.handle(), msec); }
  46. void signal();
  47. void broadcast();
  48. // C++11-compatible synonyms
  49. void notify_one() { signal(); }
  50. void notify_all() { broadcast(); }
  51. };
  52. //////////////////////////////////////////////////////////////////////////////
  53. }} // namespace
  54. #endif // header guard