【问题标题】:boost and c++11 thread compatibility [duplicate]boost和c ++ 11线程兼容性[重复]
【发布时间】:2013-08-28 07:25:43
【问题描述】:

有一段代码在 ubuntu 下使用 boost 线程运行良好。它是基本的只读数据共享多线程。我尝试使用 C++11 而不是 boost,非常基本的过渡。代码可以编译,但有一些细微的错误。使用 C++11 标准线程随机崩溃。尝试使用 valgrind drd,但很难阅读调试信息。有什么想法吗?

==19608== Conflicting load by thread 3 at 0x00643be8 size 8
==19608==    at 0x41CEBA: std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul>::operator()() (random.tcc:601)
==19608==    by 0x41CD6C: double std::generate_canonical<double, 53ul, std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul> >(std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul>&) (random.tcc:3475)
==19608==    by 0x41CB06: std::__detail::_Adaptor<std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul>, double>::operator()() (random.h:190)
==19608==    by 0x41C877: double std::normal_distribution<double>::operator()<std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul> >(std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul>&, std::normal_distribution<double>::param_type const&) (random.tcc:1950)
==19608==    by 0x41C688: double std::normal_distribution<double>::operator()<std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul> >(std::subtract_with_carry_engine<unsigned int, 24ul, 10ul, 24ul>&) (random.h:2196)
==19608==    by 0x41C379: nrand(double, double) (NRand.cpp:8)
==19608==    by 0x416F78: ClassDef::set_x() (LoanDef.h:407)
==19608==    by 0x4168CA: Sim(std::vector<ClassDef, std::allocator<ClassDef> >&, Assumption&, std::unordered_map<std::string, std::vector<double, std::allocator<double> >, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::vector<double, std::allocator<double> > > > >&, unsigned int, NextStepCalcML&, std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > >&, unsigned int, unsigned int) (NewSim.cpp:105)
==19608==    by 0x411A09: _ZNSt12_Bind_simpleIFPFvRSt6vectorI7LoanDefSaIS1_EER10AssumptionRSt13unordered_mapISsS0_IdSaIdEESt4hashISsESt8equal_toISsESaISt4pairIKSsS9_EEEjR14NextStepCalcMLRS0_IS0_IS9_SaIS9_EESaISN_EEjjESt17reference_wrapperIS3_EST_IS5_EST_ISI_EjST_ISK_EST_ISP_EjjEE9_M_invokeIILm0ELm1ELm2ELm3ELm4ELm5ELm6ELm7EEEEvSt12_Index_tupleIIXspT_EEE (functional:1732)
==19608==    by 0x4116AE: std::_Bind_simple<void (*()(std::reference_wrapper<std::vector<LoanDef, std::allocator<LoanDef> > >, std::reference_wrapper<Assumption>, std::reference_wrapper<std::unordered_map<std::string, std::vector<double, std::allocator<double> >, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::vector<double, std::allocator<double> > > > > >, unsigned int, std::reference_wrapper<NextStepCalcML>, std::reference_wrapper<std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > > >, unsigned int, unsigned int))(std::vector<LoanDef, std::allocator<ClassDef> >&, Assumption&, std::unordered_map<std::string, std::vector<double, std::allocator<double> >, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::vector<double, std::allocator<double> > > > >&, unsigned int, NextStep&, std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > >&, unsigned int, unsigned int)>::operator()() (functional:1720)
==19608==    by 0x411647: std::thread::_Impl<std::_Bind_simple<void (*()(std::reference_wrapper<std::vector<ClassDef, std::allocator<ClassDef> > >, std::reference_wrapper<Assumption>, std::reference_wrapper<std::unordered_map<std::string, std::vector<double, std::allocator<double> >, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::vector<double, std::allocator<double> > > > > >, unsigned int, std::reference_wrapper<NextStep>, std::reference_wrapper<std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > > >, unsigned int, unsigned int))(std::vector<LoanDef, std::allocator<LoanDef> >&, Assumption&, std::unordered_map<std::string, std::vector<double, std::allocator<double> >, std::hash<std::string>, std::equal_to<std::string>, std::allocator<std::pair<std::string const, std::vector<double, std::allocator<double> > > > >&, unsigned int, NextStep&, std::vector<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, std::allocator<std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > > > >&, unsigned int, unsigned int)> >::_M_run() (thread:115)

谢谢。顺便说一句,我尝试使用我写的这个随机头文件,不确定它在多线程环境下是否安全。它曾经与 boost 一起工作得很好。

myrand.hpp

#ifndef NRAND_H
#define NRAND_H
#include <random>

double nrand(double mean = 0., double sd = 1.);
double urand(double a=0., double b=0.);

#endif

和 myrand.cpp

#include "NRand.h"
using namespace std;

double nrand(double mean, double sd) {
    static random_device rd;
    static subtract_with_carry_engine<unsigned,24,10,24> e(rd());
    normal_distribution<> dist(mean, sd);
    return dist(e);
}

double urand(double a, double b) {
    static random_device rd;
    static subtract_with_carry_engine<unsigned,24,10,24> e(rd());
    uniform_real_distribution<> dist(a, b);
    return dist(e);
}

非常感谢。

【问题讨论】:

  • @Rapptz 这个问题在这里无疑很有用,但没有答案。
  • 您的 nrandurand 绝不是线程安全的,并且会调用未定义的行为。
  • 我应该让随机设备和引擎成为非静态的吗?那么如何去除开销呢?谢谢。
  • 您可能要记住的另一件事是,如果线程尚未加入或分离,std::thread 的析构函数会调用std::terminate(相关:stackoverflow.com/questions/4508181/…

标签: c++ multithreading boost c++11 random


【解决方案1】:

恕我直言,您的代码不是线程安全的,因此在 C++11 下不应正常工作。我认为问题在于static 变量rde 是全局变量但不受保护(受互斥体),因此并发调用会竞争。

据推测,您可以通过使这些变量thread_local 使这段代码线程安全,但我没有经验。

【讨论】:

  • 谢谢,它适用于 thread_local。
猜你喜欢
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 2012-01-24
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
相关资源
最近更新 更多