Consider the following simplistic example of condition variables:
bool pause = true;
boost::mutex::scoped_lock lock(m_mutex);
while (!pause) cv.wait(lock);
and
boost::mutex::scoped_lock lock(m_mutex);
pause = false;
cv.notify_one();
Do we essentially need the scoped_lock
or any other lock here, if we are running the code on a processor which supports updates on byte-granularity. This essentially means that the assignment of bools are atomic which is typically the case with x86 processors.
Has it got something to do with syncing of the variable in the case when the two threads are running on two different processors and have separate caches?
while (!pause) cv.wait(lock);
can be writtencv.wait(lock, [&]{ return pause; });
– Maniacpause
is protected with the lock currently. Do you ask if you can remove the lock guard ? – Maniacstd::atomic<bool>
. – Maniac