测试环境gcc4.8.2
iterator find ( const key_type& k );
const_iterator find ( const key_type& k ) const;
Unordered_map有两个对应的find函数 我推断第二个是安全的,第一个经过实测不是线程安全
auto iter = _map.find(key)
在openmp多线程环境下出core,感谢glog的错误信息输出
*** Aborted at 1412933723 (unix time) try "date -d @1412933723" if you are using GNU date ***
PC: @ 0x8d1d65 std::equal_to<>::operator()()
*** SIGSEGV (@0x8) received by PID 3216 (TID 0x7f7c3902d700) from PID 8; stack trace: ***
@ 0x7f7c4b769150 (unknown)
@ 0x8d1d65 std::equal_to<>::operator()()
@ 0x8e231b std::__detail::_Equal_helper<>::_S_equals()
@ 0x8dbbb0 std::__detail::_Hashtable_base<>::_M_equals()
@ 0x8d1e63 std::_Hashtable<>::_M_find_before_node()
@ 0x8c432e std::_Hashtable<>::_M_find_node()
@ 0x8b2bca std::_Hashtable<>::find()
@ 0x89adcd std::unordered_map<>::find()
@ 0x87af21 gezi::LruMap<>::find()
@ 0x85c6ab gezi::TimerMap<>::count()
@ 0x7ee705 run()
@ 0x7f1893 _Z3runv._omp_fn.3
@ 0x7f7c4a65641a gomp_thread_start
@ 0x7f7c4b7611c1 start_thread
@ 0x7f7c49b6c0ad __clone
Segmentation fault
由于我可能需要修改iter->second,因此不能改用const_iterator
增加
iterator iter;
#pragma omp critical
iter = _map.find(key);
程序运行正常了