counter.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Distributed under the Boost Software License, Version 1.0. (See
  2. // accompanying file LICENSE_1_0.txt or copy at
  3. // http://www.boost.org/LICENSE_1_0.txt)
  4. // (C) Copyright 2013 Vicente J. Botet Escriba
  5. #ifndef BOOST_THREAD_COUNTER_HPP
  6. #define BOOST_THREAD_COUNTER_HPP
  7. #include <boost/thread/detail/config.hpp>
  8. #include <boost/thread/detail/delete.hpp>
  9. //#include <boost/thread/mutex.hpp>
  10. //#include <boost/thread/lock_types.hpp>
  11. #include <boost/thread/condition_variable.hpp>
  12. #include <boost/chrono/duration.hpp>
  13. #include <boost/chrono/time_point.hpp>
  14. #include <boost/assert.hpp>
  15. #include <boost/config/abi_prefix.hpp>
  16. namespace boost
  17. {
  18. namespace detail {
  19. struct counter
  20. {
  21. condition_variable cond_;
  22. std::size_t value_;
  23. counter(std::size_t value)
  24. : value_(value)
  25. {
  26. }
  27. counter& operator=(counter const& rhs)
  28. {
  29. value_ = rhs.value_;
  30. return *this;
  31. }
  32. counter& operator=(std::size_t value)
  33. {
  34. value_ = value;
  35. return *this;
  36. }
  37. operator std::size_t() const
  38. {
  39. return value_;
  40. }
  41. operator std::size_t&()
  42. {
  43. return value_;
  44. }
  45. void inc_and_notify_all()
  46. {
  47. ++value_;
  48. cond_.notify_all();
  49. }
  50. void dec_and_notify_all()
  51. {
  52. --value_;
  53. cond_.notify_all();
  54. }
  55. void assign_and_notify_all(counter const& rhs)
  56. {
  57. value_ = rhs.value_;
  58. cond_.notify_all();
  59. }
  60. void assign_and_notify_all(std::size_t value)
  61. {
  62. value_ = value;
  63. cond_.notify_all();
  64. }
  65. };
  66. struct counter_is_not_zero
  67. {
  68. counter_is_not_zero(counter const& count) : count_(count) {}
  69. bool operator()() const { return count_ != 0; }
  70. counter const& count_;
  71. };
  72. struct counter_is_zero
  73. {
  74. counter_is_zero(counter const& count) : count_(count) {}
  75. bool operator()() const { return count_ == 0; }
  76. counter const& count_;
  77. };
  78. struct is_zero
  79. {
  80. is_zero(std::size_t& count) : count_(count) {}
  81. bool operator()() const { return count_ == 0; }
  82. std::size_t& count_;
  83. };
  84. struct not_equal
  85. {
  86. not_equal(std::size_t& x, std::size_t& y) : x_(x), y_(y) {}
  87. bool operator()() const { return x_ != y_; }
  88. std::size_t& x_;
  89. std::size_t& y_;
  90. };
  91. }
  92. } // namespace boost
  93. #include <boost/config/abi_suffix.hpp>
  94. #endif