Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Full Disclosure: Full-Disclosure

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

 

 

Full Disclosure full-disclosure RSS feed   Index | Next | Previous | View Threaded


michaelslists at gmail

Jul 2, 2009, 6:01 PM

Post #1 of 4 (301 views)
Permalink
Re: [Code-Crunchers] a simple race condition and how you'd solve it

On Fri, Jul 3, 2009 at 10:25 AM, Gadi Evron<ge[at]linuxbox.org> wrote:
> A friend recently demonstrated on his blog a simple race condition he
> encountered. He also challenged folks to solve the problem.
>
> http://www.algorithm.co.il/blogs/index.php/programming/a-simple-race-condition/
>
> There's an interesting discussion in the comments which is worth a quick
> read.
>
> Also, maybe someone here will come up with a cuter idea?

Posted my proposed solution in the comments, but will probably take a
while to be moderated.

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


>        Gadi.
> --
> Gadi Evron,
> ge[at]linuxbox.org.
>
> Blog: http://gevron.livejournal.com/

--
noon silky
http://lets.coozi.com.au/

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


Valdis.Kletnieks at vt

Jul 2, 2009, 7:04 PM

Post #2 of 4 (278 views)
Permalink
Re: [Code-Crunchers] a simple race condition and how you'd solve it [In reply to]

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..


pklanka at gmail

Jul 2, 2009, 9:04 PM

Post #3 of 4 (276 views)
Permalink
Re: [Code-Crunchers] a simple race condition and how you'd solve it [In reply to]

I may be seriously wrong here; But how about implementing a simple bool
cache as a check for cache result computation.

result = cache.select(input)
if result:
return result
resultcompute = cache.select(resultcompute)
if (resultcompute == true) {
while(!cache.select(resultcompute)) {
}
return cache.select(result)
}
if resultcompute = null {
cache.insert(resultcompute, true)
result = compute(input)
cache.insert(input, result)
cache.insert(resultcompute, false)
}
return result

I think above code would work enough. I does not remove racy condition in
totality though. E.g. For a condition if two threads are accessing boolean
cache variable at the same time. But since boolean computation and inserting
into cache is a millisecond effort, this probability of two threads coming
at this point at same time is very much reduced.

regards
Phani


On Fri, Jul 3, 2009 at 7:34 AM, <Valdis.Kletnieks[at]vt.edu> wrote:

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


fw at deneb

Jul 8, 2009, 5:35 AM

Post #4 of 4 (210 views)
Permalink
Re: [Code-Crunchers] a simple race condition and how you'd solve it [In reply to]

* Valdis Kletnieks:

> 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

Right. And in general, it doesn't make sense to reinvent the wheel.
The platform probably has got some sort of ivar or future which takes
care of the synchronization/blocking. (If it doesn't, you should
implement that abstraction first.) Unless the computation is very,
very cheap (or requests are truly random), you want other threads to
block until the result computed in the initial thread becomes
available---without busy waiting.

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

Full Disclosure full-disclosure RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.