• Advertisement
Sign in to follow this  

Haskell Concurrency

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement