예제 #1
0
파일: barrier.hpp 프로젝트: bobbyzhu/fibio
    /**
     * Block until count fibers have called `wait` or `count_down_and_wait` on `*this`.
     * When the count-th fiber calls `wait`, the barrier is reset and all waiting fibers
     * are unblocked. The reset depends on whether the barrier was constructed with a
     * completion function or not. If there is no completion function or if the completion
     * function result is `void`, the reset consists in restoring the original count.
     * Otherwise the rest consist in assigning the result of the completion function
     * (which must not be 0).
     */
    bool wait()
    {
        unique_lock<mutex> lock(m_mutex);
        unsigned int gen = m_generation;

        if (--m_count == 0) {
            m_generation++;
            m_count = static_cast<unsigned int>(fct_());
            assert(m_count != 0);
            m_cond.notify_all();
            return true;
        }

        while (gen == m_generation) m_cond.wait(lock);
        return false;
    }
예제 #2
0
 unsigned int operator()()
 {
   fct_();
   return size_;
 }