Original Post
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. 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.
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