[Haskell-cafe] Re: MVar and Par ..
haskell at list.mightyreason.com
Tue Dec 16 07:34:23 EST 2008
Mozhgan Kabiri wrote:
> Hi .. Hope you are doing well . I've just joined this group.
> Recently, I am struggling to do some simple experiment with haskell
> language about parallelism and wrong answers that we can get while using
> a shared variable .
Your goal is still unclear.
Are you trying to create an example which shows unexpected answers?
Are you trying to create an example which shows the expected answer?
> I tried to write a simple program, for example calculationg 'n=n+1' few
> times.And then I tried to do it in parallel by using 'par' and 'pseq' .
> The aim was to get the wrong answer because we have to share a variable
> here,and without using 'MVar' function we will get the wrong answer for
> the calculation .
MVar is a mutable storage cell. One can use forkIO to create IO threads which
race to change MVar, and if this is done badly then you can get unexpected answers.
One cannot use the pure "par" and "pseq" to launch IO threads, so you cannot use
"par" and "pseq" to create race conditions, so one can only get the expected answer.
> I don't know how to write it in parallel in order to get a wrong answer
> when we don't use MVar,because we have a shared variable here. I read
> about MVars as well,but also I don't know how to combine MVar and Par
> together to get the program to work.
I do not immediately see how MVar and "par" can be sensibly combined at all.
> I wrote this :
> module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n |
> x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do
> putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished"
> I want to make to work in parallel by using 'Par'.And also use MVar for
> this simple example to work.
> All of the example about MVar are a little bit complicated and I
> couldn't figure it that how can I write one,the same !
> Can any one help me with this ? I want a simple example that I can feel
> the need of MVar when I run my program in parallel and while I am using
> a shared variable.
> Regards; Mozhgan
I have run out of time, so I leave this part of your question to others.
More information about the Haskell-Cafe