Threading Questions

bitwise via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Oct 4 08:06:34 PDT 2015


On Wednesday, 30 September 2015 at 10:32:01 UTC, Jonathan M Davis 
wrote:
> On Tuesday, September 29, 2015 22:38:42 Johannes Pfau via 
> Digitalmars-d-learn wrote:
>> [...]
>
> What I took from the answers to that SO question was that in 
> general, it really doesn't matter whether a condition variable 
> has spurious wakeups. You're going to have to check that the 
> associated bool is true when you wake up anyway. Maybe without 
> spurious wakeups, it wouldn't be required if only one thread 
> was waiting for the signal, but you'd almost certainly still 
> need an associated bool in case it becomes true prior to 
> waiting. In addition, if you want to avoid locking up your 
> program, it's ferquently the case that you want a timed wait so 
> that you can check whether the program is trying to exit (or at 
> least that the thread in question is being terminated), and 
> you'd need a separate bool in that case as well so that you can 
> check whether the condition has actually been signaled. So, 
> ultimately, while spurious wakeups do seem wrong from a 
> correctness perspective, when you look at what a condition 
> variable needs to do, it usually doesn't matter that spurious 
> wakeups exist, and a correctly used condition variable will 
> just handle spurious wakeups as a side effect of how it's used.
>
> - Jonathan M Davis

Yea, I guess you're right. The class in the example I posted was 
a crude reproduction of something I'm using right now in another 
project:

http://codepad.org/M4fVyiXf

I don't think it would make a difference whether it woke up 
randomly or not. I've been using this code regularly with no 
problems.

    Bit


More information about the Digitalmars-d-learn mailing list