map find 是線程安全的嗎


測試環境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);

   

程序運行正常了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM