[Haskell-cafe] Race condition possible?

Peter Verswyvelen bugfact at gmail.com
Sat Feb 14 15:53:21 EST 2009


If you have two streams of time/value pairs - using MVars as write-once
sampling variables - and both streams are fed from another thread (e.g.
timers firing), and you want to merge these two streams into a single stream
with monotonic time stamps, then you want to be able to check if at time t
an occurrence exists in a stream. In the case an old time was read but not
yet written to an mvar, this could lead to the merged stream not being
monotonic. At least in my C# prototype that was the case, I used many very
high frequency timers to stress test the merger, and this bug popped up. I
found similar code in the Reactive library, but Reactive is much more clever
operationally (and semantically) than my little prototype so it might not be
a problem. But if it is, I guess it can be solved by introducing another
MVar to indicate "I'm reading time", at least I solved it in the C#
prototype in that way.


On Sat, Feb 14, 2009 at 8:01 PM, Paul Johnson <paul at cogito.org.uk> wrote:

> I'm not entirely sure what you are trying to achieve here.  Presumably you
> want v to contain the (value, time) pair as soon as possible after time "t".
>  Of course it won't be instantaneous.  So another thread could observe that
> at time (t+delta) the variable "v" does not yet contain (x,t).  Is this a
> problem?
>
> Atomic transactions won't work because "getCurrentTime" is of type "IO
> Time", whereas anything inside "atomic" has to be of type "STM a".
>
> In theory you could lock out context switches by messing around with the
> GHC runtime, but if you are running on a multicore machine then that won't
> work either.
>
> Paul.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090214/7fec080e/attachment.htm


More information about the Haskell-Cafe mailing list