Quantcast
Viewing latest article 1
Browse Latest Browse All 2

Answer by Zan Lynx for Condition variable spurious wakeup/round robin tasks

You should build your code so that without any condition variable it would still work. If it would work if it locked a mutex, checked for the condition, unlocked the mutex and immediately repeated the loop, then it will work with a condition variable, only more efficiently.

An implementation could wake your condition variables every 10 milliseconds, if it wanted to for some reason, or if there was some weird interaction with instructions like MWAIT, cache lines and Transactional Locks or some other strange hardware feature.

I seem to recall reading about an architecture that would signal wait events on memory locations whenever any part of that cache line was written. And the thread library didn't pad the structure enough for the cache line size.

So YES you might have to handle hundreds of wakes per second and your code should still work correctly.

From what I see of what you've written, you have only one place that you are checking the predicate, which is the condition that must be true to proceed past the condition. And before every notify you should have updated the predicate. The predicate must always be written or read while protected by the same lock used by the condition variable.

If you want different behavior you might rather use a semaphore.


Viewing latest article 1
Browse Latest Browse All 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>