[Haskell-cafe] Relaxing atomicity of STM transactions

Felipe Lessa felipe.lessa at gmail.com
Tue Sep 28 09:54:55 EDT 2010

On Tue, Sep 28, 2010 at 10:41 AM, Peter Robinson <thaldyron at gmail.com> wrote:
> readTVarIO :: TVar a -> IO a

One needs to know if it is ok to wrap this IO action into an STM
action.  For example,

> data I a = I a
> looselyReadTVar :: TVar a -> STM a
> looselyReadTVar tvar =
>   let v = unsafePerformIO (I <$> readTVarIO tvar)
>   in case v of I x -> return x

The 'case' is needed because otherwise the TVar would be read
only when its value was requested, and we want to keep the
ordering.  The 'I' datatype is used to avoid evaluating the
user's value (which could even be 'undefined').

Note that this function can be used on any monad, but I don't
think that is a good idea =).



More information about the Haskell-Cafe mailing list