How to handle entries of concurrent iterators concurrently? #1377
Replies: 1 comment 5 replies
-
In general, the code above is not thread-safe because multiple threads can assign to the same |
Beta Was this translation helpful? Give feedback.
-
A
tbb::concurrent_unordered_map<string, string>::iterator
object is apparently a solist_iterator.Since none of the methods of
concurrent_unordered_map
that obtain iterators are declared unsafe, I assume multiple such iterators of the same element are meant to be usable in multiple threads without extra synchronization by the user.But is something like
really somehow thread-safe?
Or do I need to modify the iterator's value by using a different method?
[*] The only way I see in which the above code could be thread-safe is that
concurrentIt
actually contains a local copy of its entry inmap
, which is synchronized withmap
(hopefully based on modification timestamps so that only latest changes persist) at points in time where its values are not being modified by the single thread which createdconcurrentIt
and is supposed to use it (e.g. when one of its methods is being called). Each synchronization would have to take all iterators by all threads of the same entry into account, so there should be an extra "not currently being modified" buffer for each thread and iterator to be requestable from other threads. (This wouldn't solve that obsolete data could be exposed to different threads, though, with no apparent way to update it when no method of the iterator was used for modification.)I have similar code that upon execution seems to be reliable, but it just doesn't look reliable and I wonder whether or how I should improve it. Using extra mutexes for synchronization would be a terrible solution, since that would greatly impair performance for an issue that seems to never happen.
Is [*] really how it works, with the "obsolete data" issue somehow being solved, and is my highlighted code thereby perfectly fine?
Beta Was this translation helpful? Give feedback.
All reactions