Is there any way to check if an iterator is valid?

后端 未结 5 747
面向向阳花
面向向阳花 2021-01-06 15:21

For two threads manipulating a container map for example, what the correct way to test whether an iterator still valid (for performance reason) ?
Or would be of only in

5条回答
  •  广开言路
    2021-01-06 15:50

    If your STL does not offer a thread safe std::map, Intel's TBB offers a thread-safe concurrent_hash_map (pages 60 and 68).

    Putting thread safety issues aside, std::map does guarantee that deletions do not invalidate iterators other than the one being deleted. Unfortunately there isn't a is_iterator_valid() method to validate iterators that you hold.

    It may be possible to implement something like hazard pointers, and TBB has some workarounds to the problem as well.

提交回复
热议问题