<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
<br>Hi , <br><br>Yeah , first I want to get unexpected answer ! And then I want to prove that when I use MVar I will get the write answer.I want to show that when we have a shared variable, we should use MVar.Besides we use for example two processors to do different tasks.But the point is these processors have to share a variable and I want to show it with MVar.But I don't know how to write a simple code to show it while undrestand it.<br><br>Mozhgan<br><br>> To: haskell-cafe@haskell.org<br>> From: haskell@list.mightyreason.com<br>> Date: Tue, 16 Dec 2008 12:34:23 +0000<br>> Subject: [Haskell-cafe] Re: MVar and Par ..<br>> <br>> Mozhgan Kabiri wrote:<br>> > Hi .. Hope you are doing well . I've just joined this group.<br>> <br>> Hi.<br>> <br>> > Recently, I am struggling to do some simple experiment with haskell <br>> > language about parallelism and wrong answers that we can get while using <br>> > a shared variable .<br>> <br>> Your goal is still unclear.<br>> Are you trying to create an example which shows unexpected answers?<br>> Are you trying to create an example which shows the expected answer?<br>> <br>> > I tried to write a simple program, for example calculationg 'n=n+1' few <br>> > times.And then I tried to do it in parallel by using 'par' and 'pseq' . <br>> > The aim was to get the wrong answer because we have to share a variable <br>> > here,and without using 'MVar' function we will get the wrong answer for <br>> > the calculation .<br>> <br>> MVar is a mutable storage cell. One can use forkIO to create IO threads which <br>> race to change MVar, and if this is done badly then you can get unexpected answers.<br>> <br>> One cannot use the pure "par" and "pseq" to launch IO threads, so you cannot use <br>> "par" and "pseq" to create race conditions, so one can only get the expected answer.<br>> <br>> > I don't know how to write it in parallel in order to get a wrong answer <br>> > when we don't use MVar,because we have a shared variable here. I read <br>> > about MVars as well,but also I don't know how to combine MVar and Par <br>> > together to get the program to work.<br>> <br>> I do not immediately see how MVar and "par" can be sensibly combined at all.<br>> <br>> > I wrote this :<br>> > <br>> > module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n | <br>> > x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do <br>> > putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished"<br>> > I want to make to work in parallel by using 'Par'.And also use MVar for <br>> > this simple example to work.<br>> > All of the example about MVar are a little bit complicated and I <br>> > couldn't figure it that how can I write one,the same !<br>> > <br>> > Can any one help me with this ? I want a simple example that I can feel <br>> > the need of MVar when I run my program in parallel and while I am using <br>> > a shared variable.<br>> > <br>> > Regards; Mozhgan<br>> <br>> I have run out of time, so I leave this part of your question to others.<br>> <br>> _______________________________________________<br>> Haskell-Cafe mailing list<br>> Haskell-Cafe@haskell.org<br>> http://www.haskell.org/mailman/listinfo/haskell-cafe<br><br /><hr />Explore the seven wonders of the world <a href='http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE' target='_new'>Learn more!</a></body>
</html>