c++ - 使用 boost::ref 的段错误

标签 c++ multithreading boost

环境: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 回溯:

#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 ?? ()

最佳答案

fbitarrays 只有 10 个 vector ,而 p 迭代到 99。 当给定 fbitarrays[10][0] 的线程或任何 p>9 线程恰好首先被调度并运行时,它会尝试访问损坏的 boost::ref 时发生段错误从不存在的 vector 构建。

当使用索引访问遍历 vector 时,始终达到 vector.size() 是有帮助的。

thread_group 的使用也有错误,但这不是崩溃的原因。

关于c++ - 使用 boost::ref 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5796420/

相关文章:

C++,如何确定 Windows 进程是否正在运行?

java - 跟踪频率时线程安全哈希表放置的可扩展模式

multithreading - 有效地保护一个主要被读出而很少被写入的关键部分

c中的消费者生产者问题

c++ - 执行 make 命令创建 moses\scripts\training\memscore 时出错

c++ - 如何在不提示输入密码的情况下设置计划任务

c++ - Boost Statechart - 本地转换

c++ - 我可以在可以在 Octave 中调用的多个 C++ 函数中描述共享变量吗?

c++ - boost 目录迭代器

c++ - Boost.Locale - C++ 中的 Unicode 字符串