Boost RNG的线程安全 [英] Thread-safety of boost RNG

查看:86
本文介绍了Boost RNG的线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a loop which should be nicely parallelized by insering one 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();
      }

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

(unless I make an error each execution does not depend on others at all. Sorry that not all of code is inserted).

但是我的问题是-boost RNG线程安全吗?他们似乎将gcc引用为gcc代码,因此即使gcc代码是线程安全的,其他平台也可能不是这种情况.

However my question is - are boost RNG thread-safe? They seems to refer to gcc code for gcc so even if gcc code is thread-safe it may not be the case for other platforms.

推荐答案

浏览Boost邮件列表档案可提供:

Browsing through the Boost mailing list archives gives:

Boost.Random不保持全局 指出需要保护以免 多线程.

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

Boost.Random是线程安全的,只要 您不从访问任何给定的对象 同时两个线程. (正在访问 只要两个不同的对象就可以 因为他们不共享引擎).如果你 需要那种安全,这是 靠自己平凡地将其推翻 适当的互斥包装器.

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.

这篇关于Boost RNG的线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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