4

I'm interested to use the condition_variable to synchronize two threads ( A and B ) that don't share data. A good solution I found surfing the internet is the following one.

class WaitableCondition

I found it here : Paper link

The problem is in two code fragments, the first one follows.

   void WaitUntilTrue (void)
    {
        std::unique_lock<std::mutex> uLock(m_mutex);
        m_conditionVar.wait(uLock,[&]{return m_condIsTrue}); //<==== ?????
        if (m_autoReset) m_condIsTrue = false;
    }

Does spurious wakeup happen in the line with question marks comment ?

The same question is related to the following line code :

if (m_conditionVar.wait_for(uLock, timeoutPeriod, [&] {return m_condIsTrue;}))
....

Thanks

  • 1
    For the former, as far as you're concerned, not really, but only because of how the `wait` member works. Internal to the `wait` member a spurious wakeup can certainly happen. When it does, the predicate, `m_condIsTrue`, is checked, will potentially be found false, and `wait` will march right back into its duty of atomically releasing the mutex and waiting once-again for the condition signal. (all of that assuming I understood what you were asking, which isn't likely as-of-late). – WhozCraig Apr 27 '16 at 09:11
  • Thanks for the answer. – weblightman Apr 28 '16 at 07:41

0 Answers0