[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Full-disclosure] [Code-Crunchers] a simple race condition and how you'd solve it



On Fri, 03 Jul 2009 11:01:34 +1000, silky said:

> Basically, you just need to check if you should still be computing,
> and, at the end of computation, if your data is still "wanted".

All that does is push the race condition around.  You *still* need to
do some sort of locking around the tail end.  This is still racy:

    if (update_still_wanted) {
        stash_my_update();
        update_still_wanted = false;
    }

(Admittedly, not *as* racy, especially if you move the assignment first. But
that's still racy enough to actually *trip* on occasion - this sort of bug
is actually found at least once a month in the Linux kernel in some device
driver or other...)

And to be honest - the "best" way of fixing this is *really* going to depend on
the relative weight of locking (which can be *very* different if you have 2
threads on 2 CPUs, or 4096 threads on a 4096-core monster, or are split across
systems possibly in different countries connected by a high or maybe low speed
network), and how much effort goes into the computation, and how much
correctness matters - for some cases, you *really* want "first to finish"
(possibly due to side effects of the computation), others "any complete answer"
is good enough, etc..

Attachment: pgpR3xlO7w3h_.pgp
Description: PGP signature

_______________________________________________
Full-Disclosure - We believe in it.
Charter: http://lists.grok.org.uk/full-disclosure-charter.html
Hosted and sponsored by Secunia - http://secunia.com/