unordered_map thread safety

后端 未结 7 711
故里飘歌
故里飘歌 2020-11-30 02:12

I am changing a single thread program into multi thread using boost:thread library. The program uses unordered_map as a hasp_map for lookups. My question is..

At on

7条回答
  •  执念已碎
    2020-11-30 03:06

    The existing answers cover the main points:

    • you must have a lock to read or write to the map
    • you could use a multiple-reader / single-writer lock to improve concurrency

    Also, you should be aware that:

    • using an earlier-retrieved iterator, or a reference or pointer to an item in the map, counts as a read or write operation

    • write operations performed in other threads may invalidate pointers/references/iterators into the map, much as they would if they were done in the same thread, even if a lock is again acquired before an attempt is made to continue using them...

提交回复
热议问题