码迷,mamicode.com
首页 > 编程语言 > 详细

C++ CountDownLatch

时间:2020-10-18 09:24:14      阅读:22      评论:0      收藏:0      [点我收藏+]

标签:not   count   ++   ==   --   vat   except   void   condition   

CountDownLatch

用于等待其它线程都处于就绪状态:

#include <mutex>
#include <condition_variable>

class CountDownLatch {
public:
    CountDownLatch(uint32_t count) : m_count(count) {}

    void countDown() noexcept {
        std::lock_guard<std::mutex> guard(m_mutex);
        if (0 == m_count) {
            return;
        }
        --m_count;
        if (0 == m_count) {
            m_cv.notify_all();
        }
    }

    void await() noexcept {
        std::unique_lock<std::mutex> lock(m_mutex);
        m_cv.wait(lock, [this] { return 0 == m_count; });
    }

private:
    std::mutex m_mutex;
    std::condition_variable m_cv;
    uint32_t m_count;
};

C++ CountDownLatch

标签:not   count   ++   ==   --   vat   except   void   condition   

原文地址:https://www.cnblogs.com/HachikoT/p/13829160.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!