I have 2 threads one inserting a pair (“key/value”) into a list and the other one removing a pair from the same list.
Now I can guarantee that those threads will never be modifying the same “key/value” pair.
Is that thread safe of should I protect the list with mutexes?
thanks.
In general any sort of modifications to any of the std containers should absolutely be protected.
You could consider using a pthread_rwlock, using a read lock for reading and a write lock for writing. The nice thing about rwlocks is that you can have multiple simultaneous readers, thus reducing locking contention. Or at the very least a pthread_mutex. (Assuming Linux, of course)