mercredi 2 janvier 2019

lock_guard two critical sections with one mutex

I have Get() and Set() methods and I want to make them thread safe. Wondering if I should use two mutex, one for Get and second one for Set or can I use one mutex, as in the code below.

public:
float Get(string name){
  lock_guard<mutex> lock(mutex1);
  return value_map[name];
}

Set(string name, float value){
  lock_guard<mutex> lock(mutex1);
  value_map[name] = value;
}

private:
map<string, float> value_map;
mutex mutex1;

Aucun commentaire:

Enregistrer un commentaire