Ran*_*Guy 2 c++ multithreading boost
我们有一个使用boost的矩阵和稀疏矩阵库的程序,我们正在尝试集成boost线程.但是,当我们从单线程应用程序迁移到多线程应用程序时,我们会遇到在单线程情况下不会发生的分段错误.
我们使用gdb(在Eclipse中)进行了调试,我发现在boost代码中的函数调用期间发生了seg错误,即我尝试访问稀疏矩阵的条目,堆栈跟踪进入boost代码并死于这些文件中的某些点(并非总是相同的点).
我很困惑,因为这些矩阵在各个线程中分配,所有共享资源都受互斥锁保护.此外,我不认为线程经常导致seg错误,只有多次访问和坏数据.但是,我显然没有多线程编程的经验,所以我希望在这个领域更有经验的人可以提供一些建议.
我正在使用boost托管make系统和一个示例编译命令
g++ -DNDEBUG -I"/usr/local/include/boost-1_38" -I/usr/local/include -I"/home/scandido/workspace/BigSHOT/src" -I"/home/scandido/workspace/BigSHOT/src/base" -O3 -Wall -c -fmessage-length=0 `freetype-config --cflags` -pthread -MMD -MP -MF"src/utils/timing_info.d" -MT"src/utils/timing_info.d" -o"src/utils/timing_info.o" "../src/utils/timing_info.cpp"
Run Code Online (Sandbox Code Playgroud)
和链接器命令是
g++ -L"/usr/local/lib" -L/usr/local/lib -o"BigSHOT" ./src/utils/timing_info.o ... many more objects ... ./src/base/pomdp/policy_fn/EventDriven.o ./src/base/pomdp/policy_fn/Greedy.o ./src/anotheralgorithm.o -lboost_serialization-gcc43-mt -lpthread -lboost_thread-gcc43-mt -lboost_program_options-gcc43-mt -lboost_iostreams-gcc43-mt -lpng -lpngwriter -lz -lfreetype
Run Code Online (Sandbox Code Playgroud)
这是一个seg错误的线程的堆栈跟踪:
Thread [5] (Suspended: Signal 'SIGSEGV' received. Description: Segmentation fault.)
17 boost::numeric::ublas::mapped_matrix<bool, boost::numeric::ublas::basic_row_major<unsigned long, long>, boost::numeric::ublas::map_std<unsigned long, bool, std::allocator<std::pair<unsigned long const, bool> > > >::operator() /usr/local/include/boost-1_38/boost/numeric/ublas/matrix_sparse.hpp:377 0x000000000041c328
16 BigSHOT::Fire1FireState::get_cell() /home/scandido/workspace/BigSHOT/src/systems/fire1/pomdp/Fire1State.cpp:51 0x0000000000419a75
15 BigSHOT::Fire1SquareRegionProbObsFn::operator() /home/scandido/workspace/BigSHOT/src/systems/fire1/obs_fn/Fire1SquareRegionProbObsFn.cpp:92 0x000000000042ac37
14 BigSHOT::Fire1SquareRegionProbObsFn::operator() /home/scandido/workspace/BigSHOT/src/systems/fire1/obs_fn/Fire1SquareRegionProbObsFn.cpp:66 0x000000000042a8bf
13 BigSHOT::BayesFilterFn<BigSHOT::Fire1Belief, BigSHOT::Fire1State, BigSHOT::Fire1Action, BigSHOT::Fire1Observation>::update() /home/scandido/workspace/BigSHOT/src/base/pomdp/filter_fn/BayesFilterFn.h:50 0x0000000000445c3b
12 BigSHOT::HyperParticleFilter<BigSHOT::Fire1Belief, BigSHOT::Fire1Action, BigSHOT::Fire1Observation>::future_evolution() /home/scandido/workspace/BigSHOT/src/base/hpf/HyperParticleFilter.h:127 0x00000000004308e0
11 BigSHOT::HyperParticleFilter<BigSHOT::Fire1Belief, BigSHOT::Fire1Action, BigSHOT::Fire1Observation>::hyperfilter() /home/scandido/workspace/BigSHOT/src/base/hpf/HyperParticleFilter.h:86 0x000000000043149b
10 BigSHOT::HyperParticleFilterSystem<BigSHOT::HyperCostFn<BigSHOT::Fire1Belief, BigSHOT::Fire1Action>, BigSHOT::PolicyFn<BigSHOT::Fire1Belief, BigSHOT::Fire1Action>, BigSHOT::Fire1Belief, BigSHOT::Fire1Action, BigSHOT::Fire1Observation>::next_stage() /home/scandido/workspace/BigSHOT/src/base/hpf/HyperParticleFilter.h:189 0x0000000000446180
9 hyperfilter() /home/scandido/workspace/BigSHOT/src/anotheralgorithm.cpp:126 0x0000000000437798
8 hf_thread_wrapper() /home/scandido/workspace/BigSHOT/src/anotheralgorithm.cpp:281 0x0000000000437cd9
7 boost::_bi::list1<boost::_bi::value<int> >::operator()<void (*)(int), boost::_bi::list0>() /usr/local/include/boost-1_38/boost/bind.hpp:232 0x000000000043f25a
6 boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > >::operator() /usr/local/include/boost-1_38/boost/bind/bind_template.hpp:20 0x000000000043f298
5 boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(int), boost::_bi::list1<boost::_bi::value<int> > > >::run() /usr/local/include/boost-1_38/boost/thread/detail/thread.hpp:56 0x000000000043f2b6
4 thread_proxy() 0x00007f28241c893f
3 start_thread() 0x00007f28243d93ba
2 clone() 0x00007f2822a4ffcd
1 <symbol is not available> 0x0000000000000000
Run Code Online (Sandbox Code Playgroud)
它发生的代码行是这个块的第二个:
const_reference operator () (size_type i, size_type j) const {
const size_type element = layout_type::element (i, size1_, j, size2_);
const_subiterator_type it (data ().find (element));
Run Code Online (Sandbox Code Playgroud)
我想重申一下,seg错误并不总是出现在代码中的相同位置,而是始终在执行boost代码时执行某些操作.
在此先感谢您的帮助!
如果您损坏堆或免费存储,例如通过双重释放(或双重删除)指针,访问指针,Segfaults似乎来自其他经过良好调试的库(甚至来自标准库!)已经释放(或删除),释放(或删除)未分配的指针,使用删除,你应该使用delete [],反之亦然等.
这种崩溃通常发生在完全不同的地方,并且发生错误的时间和地点完全不同.如果你有多个线程共享的矩阵以外的变量,并且你有一个竞争条件,例如,导致你双重删除共享变量,这可能会破坏免费存储,然后导致升级矩阵内部崩溃码.
您应该通过valgrind之类的工具运行代码,以尝试跟踪堆/免费存储损坏.