boost::ref 的段错误
环境:Ubuntu Maverick (10.10)、libboost v1.42、Intel X5680 处理器
问题:将 boost::ref 与 boost::thread 和 boost::thread_group 结合使用时,在多次循环后出现分段错误(每次计数不同) 。
问题:问题可能出在哪里?提前致谢。
编译:c++ -lboost_thread -lpthread mttest.cpp -o mttest -g
示例代码:mttest.cpp
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <boost/dynamic_bitset.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;
using namespace boost::this_thread;
void thousand(boost::dynamic_bitset<> &motifarray) {
for(int i = 0; i < 1000; i++) {
motifarray[i] = 1;
}
}
int main (int argc, char* argv[]) {
boost::dynamic_bitset<> motifarray(1000);
vector< vector< boost::dynamic_bitset<> > > fbitarrays;
vector< boost::dynamic_bitset<> > curfbitarray;
for (int i = 0; i < 100; i++) {
curfbitarray.push_back(motifarray);
}
for (int i = 0; i < 10; i++) {
fbitarrays.push_back(curfbitarray);
}
for (int p = 0; p < 100; p++) {
boost::thread_group* threadpool = new boost::thread_group();
for(int j = 0; j < 10; j++) {
boost::thread a(boost::bind(&thousand, boost::ref(fbitarrays[p][j])));
threadpool->add_thread(&a);
cout << "inner loop: " << j << endl;
}
threadpool->join_all();
}
return 0;
}
gdb backtrace:
#0 0x000000000040503a in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_set (this=0x7fbcf8c1bd90)
at /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:109
#1 0x0000000000404197 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_assign (this=0x7fbcf8c1bd90, x=true)
at /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:112
#2 0x0000000000403a21 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::operator= (this=0x7fbcf8c1bd90, x=true)
at /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:97
#3 0x0000000000401ffd in thousand (motifarray=...) at mttest.cpp:16
#4 0x0000000000408994 in boost::_bi::list1<boost::reference_wrapper<boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> > > >::operator()<void (*)(boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >&), boost::_bi::list0> (this=0x7fbcf0000ed8, f=@0x7fbcf0000ed0, a=...)
at /usr/include/boost/bind/bind.hpp:253
#5 0x0000000000408947 in boost::_bi::bind_t<void, void (*)(boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >&), boost::_bi::list1<boost::reference_wrapper<boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> > > > >::operator() (this=0x7fbcf0000ed0)
at /usr/include/boost/bind/bind_template.hpp:20
#6 0x0000000000408906 in boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >&), boost::_bi::list1<boost::reference_wrapper<boost::dynamic_bitset<unsigned long, s---Type <return> to continue, or q <return> to quit---
td::allocator<unsigned long> > > > > >::run (this=0x7fbcf0000da0)
at /usr/include/boost/thread/detail/thread.hpp:56
#7 0x00007fbcfbb73230 in thread_proxy ()
from /usr/lib/libboost_thread.so.1.42.0
#8 0x00007fbcfac28971 in start_thread (arg=<value optimized out>)
at pthread_create.c:304
#9 0x00007fbcfb12c92d in clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#10 0x0000000000000000 in ?? ()
Environment: Ubuntu Maverick (10.10), libboost v1.42, Intel X5680 processors
Issue: When using boost::ref in conjunction with boost::thread and boost::thread_group, a segmentation fault appear after several loops (differing counts each time).
Question: Where might the issue be located? Thanks in advance.
To compile: c++ -lboost_thread -lpthread mttest.cpp -o mttest -g
Sample code: mttest.cpp
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <boost/dynamic_bitset.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;
using namespace boost::this_thread;
void thousand(boost::dynamic_bitset<> &motifarray) {
for(int i = 0; i < 1000; i++) {
motifarray[i] = 1;
}
}
int main (int argc, char* argv[]) {
boost::dynamic_bitset<> motifarray(1000);
vector< vector< boost::dynamic_bitset<> > > fbitarrays;
vector< boost::dynamic_bitset<> > curfbitarray;
for (int i = 0; i < 100; i++) {
curfbitarray.push_back(motifarray);
}
for (int i = 0; i < 10; i++) {
fbitarrays.push_back(curfbitarray);
}
for (int p = 0; p < 100; p++) {
boost::thread_group* threadpool = new boost::thread_group();
for(int j = 0; j < 10; j++) {
boost::thread a(boost::bind(&thousand, boost::ref(fbitarrays[p][j])));
threadpool->add_thread(&a);
cout << "inner loop: " << j << endl;
}
threadpool->join_all();
}
return 0;
}
gdb backtrace:
#0 0x000000000040503a in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_set (this=0x7fbcf8c1bd90)
at /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:109
#1 0x0000000000404197 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::do_assign (this=0x7fbcf8c1bd90, x=true)
at /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:112
#2 0x0000000000403a21 in boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >::reference::operator= (this=0x7fbcf8c1bd90, x=true)
at /usr/include/boost/dynamic_bitset/dynamic_bitset.hpp:97
#3 0x0000000000401ffd in thousand (motifarray=...) at mttest.cpp:16
#4 0x0000000000408994 in boost::_bi::list1<boost::reference_wrapper<boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> > > >::operator()<void (*)(boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >&), boost::_bi::list0> (this=0x7fbcf0000ed8, f=@0x7fbcf0000ed0, a=...)
at /usr/include/boost/bind/bind.hpp:253
#5 0x0000000000408947 in boost::_bi::bind_t<void, void (*)(boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >&), boost::_bi::list1<boost::reference_wrapper<boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> > > > >::operator() (this=0x7fbcf0000ed0)
at /usr/include/boost/bind/bind_template.hpp:20
#6 0x0000000000408906 in boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(boost::dynamic_bitset<unsigned long, std::allocator<unsigned long> >&), boost::_bi::list1<boost::reference_wrapper<boost::dynamic_bitset<unsigned long, s---Type <return> to continue, or q <return> to quit---
td::allocator<unsigned long> > > > > >::run (this=0x7fbcf0000da0)
at /usr/include/boost/thread/detail/thread.hpp:56
#7 0x00007fbcfbb73230 in thread_proxy ()
from /usr/lib/libboost_thread.so.1.42.0
#8 0x00007fbcfac28971 in start_thread (arg=<value optimized out>)
at pthread_create.c:304
#9 0x00007fbcfb12c92d in clone ()
at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112
#10 0x0000000000000000 in ?? ()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
fbitarrays
只有 10 个向量,而p
最多可迭代 99 个向量。当给定的
fbitarrays[10][0]
线程或 p>9 线程恰好首先被调度并运行时,它会在尝试访问损坏的boost::ref
时出现段错误从不存在的向量构建。使用索引访问迭代向量时,始终达到
vector.size()
会有所帮助。thread_group 的使用也有错误,但这不是这次崩溃的原因。
fbitarrays
has only 10 vectors, whilep
iterates up to 99.When the thread given
fbitarrays[10][0]
or whichever p>9 thread happens to get scheduled first, runs, it segfaults attempting access to the brokenboost::ref
built from the nonexisting vector.When iterating over a vector using indexed access, it helps to always go up to
vector.size()
.The use of thread_group has an error too, but that was not the cause of this crash.