fix doc example typo
[boost.git] / boost / thread / barrier.hpp
blob546f5e977978152a9d34fc9d18b61cc1ad7f4dee
1 // Copyright (C) 2002-2003
2 // David Moore, William E. Kempf
3 // Copyright (C) 2007-8 Anthony Williams
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_BARRIER_JDM030602_HPP
9 #define BOOST_BARRIER_JDM030602_HPP
11 #include <boost/thread/detail/config.hpp>
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/condition_variable.hpp>
15 #include <string>
16 #include <stdexcept>
18 #include <boost/config/abi_prefix.hpp>
20 namespace boost
23 class barrier
25 public:
26 barrier(unsigned int count)
27 : m_threshold(count), m_count(count), m_generation(0)
29 if (count == 0)
30 throw std::invalid_argument("count cannot be zero.");
33 bool wait()
35 boost::mutex::scoped_lock lock(m_mutex);
36 unsigned int gen = m_generation;
38 if (--m_count == 0)
40 m_generation++;
41 m_count = m_threshold;
42 m_cond.notify_all();
43 return true;
46 while (gen == m_generation)
47 m_cond.wait(lock);
48 return false;
51 private:
52 mutex m_mutex;
53 condition_variable m_cond;
54 unsigned int m_threshold;
55 unsigned int m_count;
56 unsigned int m_generation;
59 } // namespace boost
61 #include <boost/config/abi_suffix.hpp>
63 #endif