File indexing completed on 2025-01-30 09:35:34
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_FIBER_DETAIL_THREAD_BARRIER_H
0008 #define BOOST_FIBER_DETAIL_THREAD_BARRIER_H
0009
0010 #include <cstddef>
0011 #include <condition_variable>
0012 #include <mutex>
0013
0014 #include <boost/assert.hpp>
0015 #include <boost/config.hpp>
0016
0017 #include <boost/fiber/detail/config.hpp>
0018
0019 #ifdef BOOST_HAS_ABI_HEADERS
0020 # include BOOST_ABI_PREFIX
0021 #endif
0022
0023 namespace boost {
0024 namespace fibers {
0025 namespace detail {
0026
0027 class thread_barrier {
0028 private:
0029 std::size_t initial_;
0030 std::size_t current_;
0031 bool cycle_{ true };
0032 std::mutex mtx_{};
0033 std::condition_variable cond_{};
0034
0035 public:
0036 explicit thread_barrier( std::size_t initial) :
0037 initial_{ initial },
0038 current_{ initial_ } {
0039 BOOST_ASSERT ( 0 != initial);
0040 }
0041
0042 thread_barrier( thread_barrier const&) = delete;
0043 thread_barrier & operator=( thread_barrier const&) = delete;
0044
0045 bool wait() {
0046 std::unique_lock< std::mutex > lk( mtx_);
0047 const bool cycle = cycle_;
0048 if ( 0 == --current_) {
0049 cycle_ = ! cycle_;
0050 current_ = initial_;
0051 lk.unlock();
0052 cond_.notify_all();
0053 return true;
0054 }
0055 cond_.wait( lk, [&](){ return cycle != cycle_; });
0056 return false;
0057 }
0058 };
0059
0060 }}}
0061
0062 #endif