equal
deleted
inserted
replaced
|
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) |
|
7 |
|
8 #ifndef BOOST_BARRIER_JDM030602_HPP |
|
9 #define BOOST_BARRIER_JDM030602_HPP |
|
10 |
|
11 #include <boost/thread/detail/config.hpp> |
|
12 |
|
13 #include <boost/thread/mutex.hpp> |
|
14 #include <boost/thread/condition_variable.hpp> |
|
15 #include <string> |
|
16 #include <stdexcept> |
|
17 |
|
18 #include <boost/config/abi_prefix.hpp> |
|
19 |
|
20 namespace boost |
|
21 { |
|
22 |
|
23 class barrier |
|
24 { |
|
25 public: |
|
26 barrier(unsigned int count) |
|
27 : m_threshold(count), m_count(count), m_generation(0) |
|
28 { |
|
29 if (count == 0) |
|
30 throw std::invalid_argument("count cannot be zero."); |
|
31 } |
|
32 |
|
33 bool wait() |
|
34 { |
|
35 boost::mutex::scoped_lock lock(m_mutex); |
|
36 unsigned int gen = m_generation; |
|
37 |
|
38 if (--m_count == 0) |
|
39 { |
|
40 m_generation++; |
|
41 m_count = m_threshold; |
|
42 m_cond.notify_all(); |
|
43 return true; |
|
44 } |
|
45 |
|
46 while (gen == m_generation) |
|
47 m_cond.wait(lock); |
|
48 return false; |
|
49 } |
|
50 |
|
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; |
|
57 }; |
|
58 |
|
59 } // namespace boost |
|
60 |
|
61 #include <boost/config/abi_suffix.hpp> |
|
62 |
|
63 #endif |