c – 如何使用增压屏障

c – 如何使用增压屏障,第1张

概述什么是boost:barrier,如何使用这种boost方法.可以给我一个明确的例子,因为我发现了以下几个例子: bool wait() { boost::mutex::scoped_lock lock(m_mutex); unsigned int gen = m_generation; if (--m_count == 0) 什么是boost:barrIEr,如何使用这种boost方法.可以给我一个明确的例子,因为我发现了以下几个例子:
bool wait()    {        boost::mutex::scoped_lock lock(m_mutex);        unsigned int gen = m_generation;        if (--m_count == 0)        {            m_generation++;            m_count = m_threshold;            m_cond.notify_all();            return true;        }        while (gen == m_generation)            m_cond.wait(lock);        return false;    }

在上面的代码中:m_cond.notify_all();是进入其他等待线程吗?
你能否明确说明屏障功能?谢谢.

解决方法 notify_all,通知等待线程.

A barrIEr is a simple concept. Also kNown as a rendezvous,it is a
synchronization point between multiple threads. The barrIEr is
configured for a particular number of threads (n),and as threads
reach the barrIEr they must wait until all n threads have arrived.
Once the n-th thread has reached the barrIEr,all the waiting threads
can proceed,and the barrIEr is reset.

简单的例子.只有当3个线程在屏障上调用等待功能时,才会输出当前值.

#include <boost/thread.hpp>#include <boost/thread/barrIEr.hpp>#include <boost/bind.hpp>#include <boost/atomic.hpp>boost::mutex io_mutex;voID thread_fun(boost::barrIEr& cur_barIEr,boost::atomic<int>& current){    ++current;    cur_barIEr.wait();    boost::lock_guard<boost::mutex> locker(io_mutex);    std::cout << current << std::endl;}int main(){    boost::barrIEr bar(3);    boost::atomic<int> current(0);    boost::thread thr1(boost::bind(&thread_fun,boost::ref(bar),boost::ref(current)));    boost::thread thr2(boost::bind(&thread_fun,boost::ref(current)));    boost::thread thr3(boost::bind(&thread_fun,boost::ref(current)));    thr1.join();    thr2.join();    thr3.join();}
总结

以上是内存溢出为你收集整理的c – 如何使用增压屏障全部内容,希望文章能够帮你解决c – 如何使用增压屏障所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1249770.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存