本文介绍了在C ++ 11中实现boost :: barrier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试摆脱所有Boost参考的项目,并切换到纯C ++ 11.

I've been trying to get a project rid of every boost reference and switch to pure C++11.

在某一时刻,将创建线程工作程序,这些线程工作程序会等待发出"go"命令的障碍,进行工作(通过N个线程扩展),并在所有工作程序完成时进行同步.基本思想是主循环给出执行顺序(boost :: barrier .wait()),并等待具有相同功能的结果.

At one point, thread workers are created which wait for a barrier to give the 'go' command, do the work (spread through the N threads) and synchronize when all of them finish. The basic idea is that the main loop gives the go order (boost::barrier .wait()) and waits for the result with the same function.

我在另一个项目中实现了一个基于Boost版本的定制屏障,并且一切运行正常.实现如下:

I had implemented in a different project a custom made Barrier based on the Boost version and everything worked perfectly. Implementation is as follows:

Barrier.h:

class Barrier {
public:
    Barrier(unsigned int n);
    void Wait(void);
private:
    std::mutex counterMutex;
    std::mutex waitMutex;

    unsigned int expectedN;
    unsigned int currentN;
};

Barrier.cpp

Barrier::Barrier(unsigned int n) {
    expectedN = n;
    currentN = expectedN;
}

void Barrier::Wait(void) {
    counterMutex.lock();

    // If we're the first thread, we want an extra lock at our disposal

    if (currentN == expectedN) {
        waitMutex.lock();
    }

    // Decrease thread counter

    --currentN;

    if (currentN == 0) {
        currentN = expectedN;
        waitMutex.unlock();

        currentN = expectedN;
        counterMutex.unlock();
    } else {
        counterMutex.unlock();

        waitMutex.lock();
        waitMutex.unlock();
    }
}

此代码已在iOS和Android的NDK上使用,没有任何问题,但是在Visual Studio 2013项目上尝试使用此代码时,似乎只有锁定互斥锁的线程才能对其进行解锁(断言:解锁未拥有的互斥锁).

This code has been used on iOS and Android's NDK without any problems, but when trying it on a Visual Studio 2013 project it seems only a thread which locked a mutex can unlock it (assertion: unlock of unowned mutex).

我可以使用适用于C ++ 11的任何非旋转(阻塞,例如这一步)障碍吗?我只能找到使用忙碌等待的障碍,这是我要防止的事情(除非确实没有理由).

Is there any non-spinning (blocking, such as this one) version of barrier that I can use that works for C++11? I've only been able to find barriers which used busy-waiting which is something I would like to prevent (unless there is really no reason for it).

推荐答案

class Barrier {
public:
    explicit Barrier(std::size_t iCount) :
      mThreshold(iCount),
      mCount(iCount),
      mGeneration(0) {
    }

    void Wait() {
        std::unique_lock<std::mutex> lLock{mMutex};
        auto lGen = mGeneration;
        if (!--mCount) {
            mGeneration++;
            mCount = mThreshold;
            mCond.notify_all();
        } else {
            mCond.wait(lLock, [this, lGen] { return lGen != mGeneration; });
        }
    }

private:
    std::mutex mMutex;
    std::condition_variable mCond;
    std::size_t mThreshold;
    std::size_t mCount;
    std::size_t mGeneration;
};

这篇关于在C ++ 11中实现boost :: barrier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:44
查看更多