在线程之间实现同步障碍的最佳方法是什么

时间:2016-08-17 14:50:10

标签: c++ multithreading c++11

运行多个线程我需要保证我的每个线程都会在继续之前达到某个点。我需要实施一种障碍。考虑一个可以从多个线程运行的函数func

void func()
{
  operation1();
  // wait till all threads reached this point 
  operation2();
}

使用C ++ 11和VS12实现此障碍的最佳方法是什么,如果需要可以考虑提升。

3 个答案:

答案 0 :(得分:8)

您可以使用boost::barrier
不幸的是,线程障碍概念本身不是c ++ 11或visual c ++的一部分 在纯c ++ 11中,您可以使用condition variable和计数器。

#include <iostream>
#include <condition_variable>
#include <thread>
#include <chrono>

class my_barrier
{

 public:
    my_barrier(int count)
     : thread_count(count)
     , counter(0)
     , waiting(0)
    {}

    void wait()
    {
        //fence mechanism
        std::unique_lock<std::mutex> lk(m);
        ++counter;
        ++waiting;
        cv.wait(lk, [&]{return counter >= thread_count;});
        cv.notify_one();
        --waiting;
        if(waiting == 0)
        {
           //reset barrier
           counter = 0;
        }
        lk.unlock();
    }

 private:
      std::mutex m;
      std::condition_variable cv;
      int counter;
      int waiting;
      int thread_count;
};

int thread_waiting = 3;
my_barrier barrier(3);


void func1()
{
    std::this_thread::sleep_for(std::chrono::seconds(3));
    barrier.wait();
    std::cout << "I have awakened" << std::endl;
}

void func2()
{
    barrier.wait();
    std::cout << "He has awakened!!" << std::endl;
}

int main() {
    std::thread t1(func1);  
    std::thread t2(func2);
    std::thread t3(func2);
    t1.join();
    t2.join();
    t3.join();
}

每个线程等待直到满足谓词。最后一个线程将使谓词有效,并允许等待线程继续。如果你想重用 屏障(例如多次调用函数),你需要另一个 变量来重置计数器。

目前的实施是有限的。两次调用func();func();可能不会使线程第二次等待。

答案 1 :(得分:2)

一个选项可能是使用OpenMP框架。

#include <omp.h>

void func()
{
  #pragma omp parallel num_threads(number_of_threads)
  {
    operation1();

    #pragma omp barrier
    // wait till all threads reached this point 

    operation2();
  }
}

使用-fopenmp

编译代码

答案 2 :(得分:0)

解决方案

#include <cassert>
#include <condition_variable>

class Barrier
{

public:

    Barrier(std::size_t nb_threads)
        : m_mutex(),
        m_condition(),
        m_nb_threads(nb_threads)
    {
        assert(0u != m_nb_threads);
    }

    Barrier(const Barrier& barrier) = delete;

    Barrier(Barrier&& barrier) = delete;

    ~Barrier() noexcept
    {
        assert(0u == m_nb_threads);
    }

    Barrier& operator=(const Barrier& barrier) = delete;

    Barrier& operator=(Barrier&& barrier) = delete;

    void Wait()
    {
        std::unique_lock< std::mutex > lock(m_mutex);

        assert(0u != m_nb_threads);

        if (0u == --m_nb_threads)
        {
            m_condition.notify_all();
        }
        else
        {
            m_condition.wait(lock, [this]() { return 0u == m_nb_threads; });
        }
    }

private:

    std::mutex m_mutex;

    std::condition_variable m_condition;

    std::size_t m_nb_threads;
};

示例

#include <chrono>
#include <iostream>
#include <thread>

Barrier barrier(2u);

void func1()
{
    std::this_thread::sleep_for(std::chrono::seconds(3));
    barrier.Wait();
    std::cout << "t1 awakened" << std::endl;
}

void func2()
{
    barrier.Wait();
    std::cout << "t2 awakened" << std::endl;
}

int main()
{
    std::thread t1(func1);  
    std::thread t2(func2);
    t1.join();
    t2.join();

    return 0;
}

在线试用WandBox