Segfault с бустом :: ref - PullRequest
       2

Segfault с бустом :: ref

0 голосов
/ 27 апреля 2011

Среда: 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 ?? ()

1 Ответ

3 голосов
/ 27 апреля 2011

fbitarrays имеет только 10 векторов, а p повторяется до 99. Когда поток, получивший fbitarrays[10][0] или какой-либо другой поток p> 9, получит первый запланированный поток, запускается, он вызывает ошибки при попытке доступа к сломанному boost::ref, созданному из несуществующего вектора.

При итерации по вектору с использованием индексированного доступа всегда помогает значение до vector.size().

Использование thread_group также имеет ошибку, но это не было причиной этого сбоя.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...