On Wed, Sep 18, 2013 at 6:54 AM, Jonathan Wakely
On 18 September 2013 11:33, Tim Blechmann wrote:
also, checking the condition requires you to acquire a lock, which in
Right, to use a condition_variable you need the CV, a lock and a condition. If you don't have those three things you're using it wrong,
You need these 3 things, and you need to use them in the correct pattern (while-loop, not an if, etc). Typically when you have 3 interrelated things that need to be used a certain way, say maintaining certain invariants, you often wrap them together into a more easily used class. The real question is what thwarts wrapping a CV into something simpler. I think part of the answer is that you need the lock anyhow (ie to push/pull the queue element in the typical example) and you probably have the condition anyhow (queue.empty()) so all you are missing is the CV part. But it still doesn't make it easy to use. It might have been easier to somehow wrap the lock and CV together (and a lambda for the condition?) and tell people to make sure they also lock the same CV+lock thing when pushing/pulling from the queue. Of course, nothing stopping anyone from building that on top of CVs. Tony Also, dealing with spurious wake-ups is "free" if you have multiple producers/consumers - ie you wake up - was it spurious, or did another consumer pop the element before you? Doesn't matter, you need to handle them the same way. But when you are the only thread on "your side" of the queue, then spurious wake ups are just more esoterica that *the average programmer has never heard of*, and worse yet, can typically ignore for years without seeing the bug come to light (unfortunately).