mercredi 15 juin 2022

c++11 two critical sections can use nested lock_guard?

If I have two critical sections, and I make two corresponding mutex to protect each of them.(I think it is necessary to precisely control when to lock because they use in different times and scenario, Q1:is it really so?) For example:

bool a;//ignore atomic_bool, because in actual, the data structure is more complex
mutex mut_a;//for threadsafe of a
bool b;
mutex mut_b;//for threadsafe of b

And I need to implement this kind of logic:

if (lock_guard<mutex> lck(mut_a);a) { do something...
}
else {
    if (lock_guard<mutex> lck(mut_b);b) { do something...
  }
}

As seen, lock_guard(mutex) is used nested, Q2is it proper and threadsafe?

Aucun commentaire:

Enregistrer un commentaire