Sign in to follow this  
robinei

Haskell Concurrency

Recommended Posts

Tim Sweeney mentioned Haskell's STM(Software Transactional Memory) system in the recent paper everyone is talking about. I got curious about this so I tried to make something using it.
import GHC.Conc

modifyTVar f v = do x <- readTVar v
                    writeTVar v $ f x

incTVar = modifyTVar (+1)

foo v = incTVar v `par` incTVar v

var = newTVar 1

main = do
    v <- atomically var
    atomically $ foo v
    i <- atomically $ readTVar v
    putStrLn $ show i



Now this outputs "2" which is not what I want. Is there a way to make "foo" wait for all the parallell actions to finish, or is something else wrong? Well the actions aren't really parallell now since they touch the same TVar, but I wanted to see if it could resolve it. Edit: Hmm now I see that if the actions weren't commutative, the behaviour would be undefined even if it worked the way I thought, so this exact problem is not very interessting. It seems I don't know very much about this, so if someone could shed some light on concurrency in Haskell I would appreciate it. Especially STM.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this