interprocess_barrier.hpp 1.2 KB

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