mercredi 5 septembre 2018

What's the correct way t deal with spurious wakeups, in general?

Among the options below, is there any correct way to deal with spurious wakeups when using conditional variables?

1) Put the wait(unique_lock_ul) into an infinite while loop, using a boolean

unique_lock<mutex> ul(m); 
while(!full)
  cv.wait(ul);

2) Same with if

unique_lock<mutex> ul(m); 
while(!full)
  cv.wait(ul);

3) Put a condition inside the wait(), for example by using lambda functions

unique_lock<mutex> ul(m); 
cv.wait(ul, [&](){return !full;});

If none of this is correct, how does one deal with spurious wakeups easily?

I'm rather new to conditional variables in C++ and I'm not sure if some of the code I read deals with the case of spurious wakeups or not.

Aucun commentaire:

Enregistrer un commentaire