С++ 11 ошибка сегментации unordered_map

Поэтому я продолжаю получать ошибку сегментации, когда запускаю это с несколькими потоками:

class InvertedIndex {
private:
    unordered_map<string, Details> index;
    mutex indexInsert;

public:
  void addTerms(pair<string, list<string>> parsedReport) {
    unordered_map<string, Details>::iterator indexIterator;

        for (auto term = parsedReport.second.begin(); term != parsedReport.second.end(); ++term) {
            indexInsert.lock();
            indexIterator = index.find(*term);

            if (indexIterator == index.end()) {
                Details details;
                details.addOccurrence(parsedReport.first);
                index.insert(pair <string, Details> (*term, details));
            } else
                indexIterator->second.addOccurrence(parsedReport.first);

            indexInsert.unlock();
        }
    }
}

Когда я отлаживаю программу с помощью GDB, она говорит мне, что проблема связана с index.find(*term), что-то связанное с хешированием. Он работает нормально, когда я запускаю его с одним потоком, что мне кажется странным, потому что вокруг него есть блокировки.


РЕДАКТИРОВАТЬ: это трассировка стека по запросу:

Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff75de700 (LWP 3842)]
[New Thread 0x7ffff6ddd700 (LWP 3843)]
[New Thread 0x7ffff65dc700 (LWP 3844)]
[New Thread 0x7ffff5ddb700 (LWP 3845)]
[New Thread 0x7ffff55da700 (LWP 3846)]
[New Thread 0x7ffff4dd9700 (LWP 3847)]
[New Thread 0x7ffff45d8700 (LWP 3848)]
[New Thread 0x7ffff3dd7700 (LWP 3849)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff65dc700 (LWP 3844)]
0x000000000041c8bf in std::__detail::_Hash_code_base<std::string, std::pair<std::string const, Details>, std::_Select1st<std::pair<std::string const, Details> >, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::_M_bucket_index (this=0x7fffffffde10, __p=0xc0, __n=467) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable_policy.h:793
793       { return _M_h2()(__p->_M_hash_code, __n); }
(gdb) bt
#0  0x000000000041c8bf in std::__detail::_Hash_code_base<std::string, std::pair<std::string const, Details>, std::_Select1st<std::pair<std::string const, Details> >, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::_M_bucket_index (this=0x7fffffffde10, __p=0xc0, __n=467)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable_policy.h:793
#1  0x0000000000419591 in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_M_bucket_index (
    this=0x7fffffffde10, __n=0xc0) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:461
#2  0x000000000041561c in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_M_find_before_node (
    this=0x7fffffffde10, __n=57, __k="bypass", __code=1325113244381254371) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:1081
#3  0x0000000000410672 in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::_M_find_node (
    this=0x7fffffffde10, __bkt=57, __key="bypass", __c=1325113244381254371) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:478
#4  0x000000000040c9e6 in std::_Hashtable<std::string, std::pair<std::string const, Details>, std::allocator<std::pair<std::string const, Details> >, std::_Select1st<std::pair<std::string const, Details> >, std::equal_to<std::string>, std::hash<std::string>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, true, false, true>::find (this=0x7fffffffde10, __k=
    "bypass") at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/hashtable.h:939
#5  0x0000000000408e50 in InvertedIndex::addTerms (this=0x7fffffffdd00, parsedReport=...) at InvertedIndex.hpp:83
#6  0x00000000004091f2 in InvertedIndex::addReport (this=0x7fffffffdd00, fileName="/home/brodie/workspace/inverted_index/resources/test_reports/report6.xml") at InvertedIndex.hpp:104
#7  0x0000000000409e26 in InvertedIndex::loadIndex()::{lambda()#1}::operator()() const (__closure=0x6448e0) at InvertedIndex.hpp:169
#8  0x0000000000425128 in std::_Function_handler<void (), InvertedIndex::loadIndex()::{lambda()#1}>::_M_invoke(std::_Any_data const&) (__functor=...)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1926
#9  0x0000000000426ba4 in std::function<void ()>::operator()() const (this=0x6448b0) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:2311
#10 0x0000000000426df1 in std::__invoke<std::function<void ()>>(std::function<void ()>&) (__f=...) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:235
#11 0x0000000000426dc6 in std::reference_wrapper<std::function<void ()> >::operator()<>() const (this=0x7ffff65dbcc0) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:468
#12 0x0000000000426cc2 in std::_Bind_simple<std::reference_wrapper<std::function<void ()> > ()>::_M_invoke<>(std::_Index_tuple<>) (this=0x7ffff65dbcc0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1598
#13 0x0000000000426a3b in std::_Bind_simple<std::reference_wrapper<std::function<void ()> > ()>::operator()() (this=0x7ffff65dbcc0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1586
#14 0x0000000000426717 in std::_Function_handler<void (), std::reference_wrapper<std::_Bind_simple<std::reference_wrapper<std::function<void ()> > ()> > >::_M_invoke(std::_Any_data const&) (__functor=...)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1956
#15 0x0000000000426ba4 in std::function<void ()>::operator()() const (this=0x7fffd40008c8) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:2311
#16 0x000000000042681e in std::__future_base::_Task_setter<std::unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter>, void>::operator() (this=0x7fffd40008c0)
    at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:1236
#17 0x0000000000426587 in std::_Function_handler<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> (), std::__future_base::_Task_setter<std::unique_ptr<std::__future_base::_Result<void>, std::__future_base::_Result_base::_Deleter>, void> >::_M_invoke(std::_Any_data const&) (__functor=...) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1912
#18 0x000000000040bde5 in std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>::operator()() const (this=0x7ffff65dbce0)
#19 0x0000000000408568 in std::__future_base::_State_base::_M_do_set(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&) (this=0x644838,
    __f=..., __set=@0x7ffff65dbc1f: false) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:473
#20 0x000000000041b403 in std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)>::operator()(std::__future_base::_State_base*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&) const (this=0x7ffff65dbba8, __object=0x644838,
    __args#0=..., __args#1=@0x7ffff65dbc1f: false) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:554
#21 0x00000000004184fb in std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)> (std::__future_base::_State_base*, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool>)>::_M_invoke<0ul, 1ul, 2ul>(std::_Index_tuple<0ul, 1ul, 2ul>) (this=0x7ffff65dbb90) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1598
#22 0x000000000041429f in std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)> (std::__future_base::_State_base*, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool>)>::operator()() (this=0x7ffff65dbb90) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1586
#23 0x000000000040fa7f in std::__once_call_impl<std::_Bind_simple<std::_Mem_fn<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&)> (std::__future_base::_State_base*, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool>)> >() () at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/mutex:787
#24 0x00000036e920cac0 in pthread_once () at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_once.S:103
#25 0x0000000000404266 in __gthread_once (__once=0x6448a4, __func=0x403eb0 <__once_proxy@plt>) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/x86_64-redhat-linux/bits/gthr-default.h:718
#26 0x000000000040bc8d in std::call_once<void (std::__future_base::_State_base::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&), std::__future_base::_State_base* const, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >, std::reference_wrapper<bool> >(std::onc---Type <return> to continue, or q <return> to quit---
e_flag&, void (std::__future_base::_State_base::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>&, bool&), std::__future_base::_State_base* const&&, std::reference_wrapper<std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()> >&&, std::reference_wrapper<bool>&&) (__once=...,
    __f=<unknown type in /home/brodie/workspace/inverted_index/src/InvertedIndexMain, CU 0x0, DIE 0x3f48a>) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/mutex:819
#27 0x0000000000408333 in std::__future_base::_State_base::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) (this=0x644838, 
    __res=..., __ignore_failure=false) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:362
#28 0x0000000000425f92 in std::__future_base::_Task_state<void ()>::_M_run() (this=0x644838) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:1271
#29 0x0000000000425c9c in std::packaged_task<void ()>::operator()() (this=0x644928) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/future:1379
#30 0x0000000000425ad8 in any_packaged<void>::execute (this=0x644920) at ThreadPool.hpp:27
#31 0x0000000000408743 in any_packaged_task::operator() (this=0x7ffff65dbdd0) at ThreadPool.hpp:40
#32 0x0000000000404baf in Worker::operator() (this=0x641120) at ThreadPool.hpp:109
#33 0x0000000000426068 in std::_Bind_simple<Worker ()>::_M_invoke<>(std::_Index_tuple<>) (this=0x641120) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1598
#34 0x0000000000425cb9 in std::_Bind_simple<Worker ()>::operator()() (this=0x641120) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/functional:1586
#35 0x0000000000425af6 in std::thread::_Impl<std::_Bind_simple<Worker ()> >::_M_run() (this=0x641108) at /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/thread:115
#36 0x00000036ec6b2a20 in std::(anonymous namespace)::execute_native_thread_routine (__p=<optimized out>) at ../../../../../libstdc++-v3/src/c++11/thread.cc:73
#37 0x00000036e9207d15 in start_thread (arg=0x7ffff65dc700) at pthread_create.c:308
#38 0x00000036e8ef246d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:114

person kurczynski    schedule 04.02.2013    source источник
comment
Это не настоящий код. Ожидайте такого же ответа.   -  person    schedule 04.02.2013
comment
Там более 1000 строк реального кода, это то, к чему я приколол проблему. Это единственная область, где доступны эти переменные.   -  person kurczynski    schedule 04.02.2013
comment
Для чего вам нужен string term;? Вы прячете это под auto term = ....   -  person Andy Prowl    schedule 04.02.2013
comment
Ваш код выглядит нормально для меня. Я могу думать о двух возможных вариантах: 1) ваш std::string реализует COW или какую-то другую оптимизацию, которая не является потокобезопасной (маловероятно) 2) Проблема в коде, который вы нам не показали (более вероятно)   -  person    schedule 04.02.2013
comment
Имейте в виду, что ошибка сегментации не обязательно возникает в том месте, где она в конечном итоге возникает. Тем не менее рассмотрите возможность публикации фактической трассировки стека из gdb.   -  person Christian.K    schedule 04.02.2013
comment
Хорошо, я опубликовал трассировку стека. Если есть еще код, который поможет, я тоже могу его опубликовать.   -  person kurczynski    schedule 05.02.2013
comment
почему вы передаете pair<string, list<string>> по значению? Похоже, он делает тонну копирования.   -  person MSN    schedule 05.02.2013
comment
Специально не зачем, думаю, лучше было бы пройти по ссылке. Тем временем просто зациклился на этой ерунде с ошибкой сегмента. Я внесу это изменение в свой код, хотя :-)   -  person kurczynski    schedule 05.02.2013
comment
Обратите внимание, что ваш код не является безопасным для исключений: мьютекс останется заблокированным, если произойдет что-то между блокировкой и разблокировкой. Но вряд ли это может быть причиной четырех SIGSEGV.   -  person Roddy    schedule 05.02.2013
comment
Вы должны никогда блокировать операцию foo, вы должны блокировать доступ к структуре. Так что (судя по именам) это выглядит подозрительно. И не видя всего остального, что может возиться с вашей структурой данных, нет возможности что-либо диагностировать.   -  person vonbrand    schedule 05.02.2013
comment
@ Родди, я этого не знал, я постараюсь это исправить. Спасибо!   -  person kurczynski    schedule 07.02.2013
comment
@vonbrand да, это то, что я начал делать, но затем переместил его туда, где он есть сейчас, когда у меня были проблемы с этим. Я вставлю его обратно, чтобы заблокировать доступ только к структуре. Спасибо!   -  person kurczynski    schedule 07.02.2013
comment
У меня похожая проблема, но у меня много unordered_maps (около 40 тысяч карт). Я заметил, что когда происходит сбой, всегда есть два потока примерно в одной и той же точке кода, но они всегда обрабатывают разные unordered_map (каждый из них имеет свою собственную блокировку).   -  person user666412    schedule 18.08.2014


Ответы (1)


Мне кажется, что ваш тип mutex на самом деле не обеспечивает взаимного исключения. Вы не забыли использовать флаг компилятора -pthread?

person Anthony Williams    schedule 05.02.2013
comment
На самом деле я этого не делал, я компилировал с помощью g++ -Wall -c %f -std=c++0x -lxerces-c -lconfig++, а затем добавил флаг pthread в соответствии с вашим предложением g++ - Wall -c %f -std=c++0x -lxerces-c -lconfig++ -pthread и трассировка, похоже, показывает ту же проблему. - person kurczynski; 07.02.2013