Basically, I have a std::map which is shared between 2 classes. Thread1 of class A continuously monitors this map for some info and take action accordingly, and Thread2 of class B updates this map upon receiving some data on some socket.
Now how can I prevent this map from corruption, since it is modified by 2 threads of 2 different classes?
One way is that i can keep an extra bool variable per entry in map, and set it True before modifying map, and False after modification. If another thread (Thread2) finds this bool variable to be True already, then it knows that Thread1 is modifying it, and should wait. But is this method efficient?