Skip to main content
gamedev.net
🔒 Locked

Haskell Concurrency

Started by robinei Mar 27, 2006 at 1:22 PM 0 replies 800+ views
Original Post
robinei
robinei
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.