interprocess_barrier.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef BOOST_CONFIG_HPP
  14. # include <boost/config.hpp>
  15. #endif
  16. #
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. namespace boost {
  21. namespace interprocess {
  22. inline barrier::barrier(unsigned int count)
  23. : m_threshold(count), m_count(count), m_generation(0)
  24. {
  25. if (count == 0)
  26. throw std::invalid_argument("count cannot be zero.");
  27. }
  28. inline barrier::~barrier(){}
  29. inline bool barrier::wait()
  30. {
  31. scoped_lock<interprocess_mutex> lock(m_mutex);
  32. unsigned int gen = m_generation;
  33. if (--m_count == 0){
  34. m_generation++;
  35. m_count = m_threshold;
  36. m_cond.notify_all();
  37. return true;
  38. }
  39. while (gen == m_generation){
  40. m_cond.wait(lock);
  41. }
  42. return false;
  43. }
  44. } //namespace interprocess {
  45. } //namespace boost {