| Rev | Line | |
|---|
| [12] | 1 | // Copyright (C) 2002-2003 |
|---|
| 2 | // David Moore, William E. Kempf |
|---|
| 3 | // |
|---|
| 4 | // Permission to use, copy, modify, distribute and sell this software |
|---|
| 5 | // and its documentation for any purpose is hereby granted without fee, |
|---|
| 6 | // provided that the above copyright notice appear in all copies and |
|---|
| 7 | // that both that copyright notice and this permission notice appear |
|---|
| 8 | // in supporting documentation. William E. Kempf makes no representations |
|---|
| 9 | // about the suitability of this software for any purpose. |
|---|
| 10 | // It is provided "as is" without express or implied warranty. |
|---|
| 11 | |
|---|
| 12 | #include <boost/thread/detail/config.hpp> |
|---|
| 13 | #include <boost/thread/barrier.hpp> |
|---|
| 14 | #include <string> // see http://article.gmane.org/gmane.comp.lib.boost.devel/106981 |
|---|
| 15 | |
|---|
| 16 | namespace boost { |
|---|
| 17 | |
|---|
| 18 | barrier::barrier(unsigned int count) |
|---|
| 19 | : m_threshold(count), m_count(count), m_generation(0) |
|---|
| 20 | { |
|---|
| 21 | if (count == 0) |
|---|
| 22 | throw std::invalid_argument("count cannot be zero."); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | barrier::~barrier() |
|---|
| 26 | { |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | bool barrier::wait() |
|---|
| 30 | { |
|---|
| 31 | boost::mutex::scoped_lock lock(m_mutex); |
|---|
| 32 | unsigned int gen = m_generation; |
|---|
| 33 | |
|---|
| 34 | if (--m_count == 0) |
|---|
| 35 | { |
|---|
| 36 | m_generation++; |
|---|
| 37 | m_count = m_threshold; |
|---|
| 38 | m_cond.notify_all(); |
|---|
| 39 | return true; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | while (gen == m_generation) |
|---|
| 43 | m_cond.wait(lock); |
|---|
| 44 | return false; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | } // namespace boost |
|---|
Note: See
TracBrowser
for help on using the repository browser.