interprocess_barrier.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include<boost/interprocess/exceptions.hpp>
  11. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  12. #include <boost/interprocess/sync/scoped_lock.hpp>
  13. namespace boost {
  14. namespace interprocess {
  15. inline barrier::barrier(unsigned int count)
  16. : m_threshold(count), m_count(count), m_generation(0)
  17. {
  18. if (count == 0)
  19. throw std::invalid_argument("count cannot be zero.");
  20. }
  21. inline barrier::~barrier(){}
  22. inline bool barrier::wait()
  23. {
  24. scoped_lock<interprocess_mutex> lock(m_mutex);
  25. unsigned int gen = m_generation;
  26. if (--m_count == 0){
  27. m_generation++;
  28. m_count = m_threshold;
  29. m_cond.notify_all();
  30. return true;
  31. }
  32. while (gen == m_generation){
  33. m_cond.wait(lock);
  34. }
  35. return false;
  36. }
  37. } //namespace interprocess {
  38. } //namespace boost {