vendredi 7 octobre 2022

c++ block thread until condition on a thread-safe object is met

I apologise in advance if my question is a duplicate, but I was not able to find a satisfying answer to my question.

I am dealing with the following (maybe silly) issue: I am trying to synchronise two threads (A and B), and I want to block the thread A until a condition is set to true by the thread B. The "special" thing is that the condition is checked on a thread-safe object (for instance, let's consider it to be a std::atomic_bool).

My naive approach was the following:

// Shared atomic object
std::atomic_bool condition{false};

// Thread A
// ... does something
while(!condition.load()) ;  // Do nothing
// Condition is met, proceed with the job

// Thread B
// ... does something
condition.store(true);      // Unlock Thread A

but, as far as I have understood, the while implies an active wait which is undesirable.

So, I thought about having a small sleep_for as the body of the while to reduce the frequency of the active wait, but then the issue becomes finding the right waiting time that does not cause waste of time in case the condition unlocks while thread A is sleeping and, at the same time, does not make the loop to execute too often. My feeling is that this is very much dependant on the time that thread B spends before setting the condition to true, which may be not predictable.

Another solution I have found looking on other SO topics is to use a condition variable, but that would require the introduction of a mutex that is not really needed.

I am perhaps overthinking the problem, but I'd like to know if my understanding is correct, and what is the best (or correct) approach to follow, bearing in mind that I am limited to C++11.

Many thanks in advance for the help.

Aucun commentaire:

Enregistrer un commentaire