c++ - boost RNG 的线程安全

标签 c++ boost openmp boost-random

我有一个循环,应该通过插入一个 openmp pragma 很好地并行化:

  boost::normal_distribution<double> ddist(0, pow(retention, i - 1));
  boost::variate_generator<gen &, BOOST_TYPEOF(ddist)> dgen(rng, ddist);
  // Diamond                                                                
  const std::uint_fast32_t dno = 1 << i - 1;
// #pragma omp parallel for
  for (std::uint_fast32_t x = 0; x < dno; x++)
    for (std::uint_fast32_t y = 0; y < dno; y++)
      {
        const std::uint_fast32_t diff = size/dno;
        const std::uint_fast32_t x1 = x*diff, x2 = (x + 1)*diff;
        const std::uint_fast32_t y1 = y*diff, y2 = (y + 1)*diff;
        double avg =
          (arr[x1][y1] + arr[x1][y2] + arr[x2][y1] + arr[x2][y2])/4;
        arr[(x1 + x2)/2][(y1 + y2)/2] = avg + dgen();
      }

(除非我出错,否则每次执行完全不依赖于其他执行。抱歉,并非所有代码都被插入)。

但是我的问题是 - boost RNG 是线程安全的吗?他们似乎为 gcc 引用了 gcc 代码,因此即使 gcc 代码是线程安全的,但对于其他平台可能并非如此。

最佳答案

浏览 Boost 邮件列表文件可以得到:

Boost.Random does not maintain global state that would need protection from multi-threading.

Boost.Random is thread-safe as long as you don't access any given object from two threads simultaneously. (Accessing two different objects is ok, as long as they don't share an engine). If you require that kind of safety, it's trivial to roll that on your own with an appropriate mutex wrapper.

关于c++ - boost RNG 的线程安全,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2920497/

相关文章:

C++ boost 单元测试错误信息

c++ - 在 boost::asio::ip::tcp::socket 上设置非阻塞时出现错误文件描述符错误

c++ - 如何通过boost.gil从内存中加载图片?

c++ - 在 Linux 中对只读文件设置排他锁

java - 使用 SWIG 在 Java 和 C++ 之间传递缓冲区

c++ - 如何将成员函数绑定(bind)到 C++14 中的对象

c++ - 使用openMP的多线程两个功能

c++ - 为什么 C++ Boost 发行版有 `.dll` 和 `.lib` 文件?

c++ - OpenMP 并行化停止工作

c++ - OpenMP C++ 无法随处理器数量线性加速