So we created a map. We want to get some_type blah = map_variable[some_not_inserted_yet_value] this would call add new item to map if one was not previosly created. So I wonder if read is really thread safe with std::map or it is only possible to thread safly try{ ...find(..)->second...?
So we created a map. We want to get some_type blah = map_variable[some_not_inserted_yet_value] this
Share
The idea that calling
find(...)->secondis thread-safe is very dependent of your view of thread-safety. If you simply mean that it won’t crash, then as long as no one is mutating the dictionary at the same time you’re reading it, I suppose you’re okay.That said, indeed, no matter what your minimum thread safety requirements are, calling the
operator[]method is inherently not thread-safe as it can mutate the collection.If a method has no
constoverload, it means it can mutate the object, so unless the documentation indicates methods are thread-safe, the method is very unlikely to be.Then again, a
constmethod might not be thread-safe as well, because your object could depend on non-constglobal state or havemutablefields, so you’ll want to be very, very careful if you use unsynchronized classes as if they were.