[Haskell] thread-local variables (was: Re: Implicit Parameters)

Thomas Conway drtomc at gmail.com
Mon Jul 31 00:03:59 EDT 2006


Hi All,

On 7/31/06, Einar Karttunen <ekarttun at cs.helsinki.fi> wrote:
> My main objection to the TLS is that it looks like normal IO,
> but changing the thread that evaluates it can break things in ways
> that are hard to debug. E.g. we have an application that uses
> TLS and passes an IO action to a library that happens to use
> a pool of worker threads that invisible to the application.

This is why I believe transaction-local variables are a more useful concept.
You are garanteed that there is only one thread accessing them, and
they behave just like ordinary TVars except that each transaction has
its own copy.

I think you'd need an API like

    type LVar a -- "local" var
    newLVar :: a -> STM (LVar a)
    readLVar :: LVar a -> STM a
    writeLVar:: LVar a -> a -> STM ()

The argument to newLVar is an initial value that is used at the start
of each transaction.  Note that this means that the value in an LVar
does not persist between transaction. I agree that this limits their
use, but simplifies them immensely, and doesn't stand in the way their
being useful for solving a bunch of problems.

cheers,
Tom


More information about the Haskell mailing list