【发布时间】:2010-11-23 20:58:29
【问题描述】:
我们有一个程序同时使用 boost 的矩阵和稀疏矩阵库,我们正在尝试集成 boost 线程。但是,当我们从单线程应用程序迁移到多线程应用程序时,我们会遇到在单线程情况下不会发生的分段错误。
我们已经使用 gdb(在 Eclipse 中)进行了调试,我发现在 boost 代码中的函数调用期间会发生 seg 错误,即我尝试访问稀疏矩阵的条目并且堆栈跟踪进入 boost 代码并在这些文件中的某个点(并不总是同一点)死亡。
我很困惑,因为这些矩阵是在各个线程中分配的,并且所有共享资源都受到互斥锁的保护。此外,我不认为线程经常导致段错误,只是多次访问和坏数据。但是,我显然没有多线程编程的经验,所以我希望在这方面更有经验的人能够提供一些建议。
我正在使用 boost managed 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"
链接器命令是
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
这里是段错误的线程的堆栈跟踪:
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
它发生的代码行是这个块的第二行:
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));
我想重申一下,段错误并不总是发生在代码中的同一位置,而是总是在执行 boost 代码中的某些内容时发生。
提前感谢您的帮助!
【问题讨论】:
-
首先确保您的应用程序的所有是使用编译器的-pthread选项编译的。每一个目标文件。如果崩溃仍然存在,请发布实际的回溯。
-
我认为我有正确的编译选项,但我不确定。 “-pthread”在那里。我在问题文本中添加了示例,它看起来对吗?
-
我还添加了堆栈跟踪。谢谢。
标签: c++ multithreading boost