如何使用升压屏障 [英] how to use boost barrier

查看:86
本文介绍了如何使用升压屏障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是升压:障碍,如何使用这种方法提升的。你能给我一个明确的例子,因为我发现下面的例子:

What is boost:barrier, how to use this kind of boost method. Could you give me a clear example since I found the examples on the following:

    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;
    }

在上面的codeS:m_cond.notify_all();二是进入其他等待线程?
你能告诉我关于明确屏障功能?谢谢你。

In the above codes: m_cond.notify_all();is to enter into other waiting threads? Could you tell me clearly about barrier functionality? Thank you.

推荐答案

notify_all,通知等待线程。

notify_all, notified awaiting threads.

一个障碍是一个简单的概念。也称为集合,它是一个
  多线程之间的同步点。该屏障
  配置成用于螺纹(n)的特定号码,并作为线程
  到达他们必须等待,直到所有n个线程已经到达屏障。
  一旦第n线程已到达屏障,所有等待的线程
  可以继续,并且阻挡复位。

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线程调用的屏障等待功能仅所输出的。

Simple example. value of the current will be outputed only when 3 threads call wait function on barrier.

#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(bar), boost::ref(current)));
    boost::thread thr3(boost::bind(&thread_fun, boost::ref(bar), boost::ref(current)));
    thr1.join();
    thr2.join();
    thr3.join();
}

这篇关于如何使用升压屏障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆