jeudi 12 août 2021

Granting priority access to a std::mutex

I currently have two functions like this that share the same mutex.

void fooA()
{
   {
     std::lock_guard<std::mutex> guard(myMutex);
     doWork()
   }
}

void fooB()
{
   {
     std::lock_guard<std::mutex> guard(myMutex);
     doWork()
   }
}

My question is if it is possible to specify that fooA() mutex should be given priority/preference ? I understand that mutexes is first come first serve. Say there are four threads t1,t2,t3,t4. Now suppose thread t1 currently owns the mutex in fooB() and t2 and t3 are waiting in line for the mutex in fooB() whereas thread t4 is waiting in line for the mutex at fooA(). What I would like is for thread t4 to be given priority next since its accessing fooA() and lets t2,t3 continue waiting. All of this should be done after t1 releases the mutex

Aucun commentaire:

Enregistrer un commentaire