<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
Hi ..<br><br>Thanks ..<br>But I keep get error when I run the program in order to see the result . It says 'var' is not in the scope.<br><br>Mozhgan<br><br><hr id="stopSpelling">Date: Tue, 16 Dec 2008 05:54:40 -0700<br>From: lrpalmer@gmail.com<br>To: mozhgan_kch@hotmail.com<br>Subject: Re: [Haskell-cafe] Re: MVar and Par ..<br>CC: haskell@list.mightyreason.com; haskell-cafe@haskell.org<br><br>Okay, that's a bit clearer.&nbsp; Control.Parallel is not what you want; that is for parallelizing pure code (and is very nice inside its little domain).&nbsp; But you want concurrent code: multiple threads that change state at the same time.&nbsp; That is in Control.Concurrent.<br>
<br>In particular, the "shared variable" is Data.IORef.<br><br>Simple example from which you should be able to extrapolate.<br><br><font face="courier new,monospace">import Control.Concurrent<br>import Control.Concurrent.MVar<br>
import Data.IORef<br><br>threadBody = do<br>&nbsp;&nbsp;&nbsp; x &lt;- readIORef var<br>&nbsp;&nbsp;&nbsp; writeIORef var (x+1)<br><br>main = do<br>&nbsp;&nbsp;&nbsp; var &lt;- newIORef 0<br>&nbsp;&nbsp;&nbsp; -- spawn two threads<br>&nbsp;&nbsp;&nbsp; forkIO threadBody<br>&nbsp;&nbsp;&nbsp; forkIO threadBody<br>
<br>&nbsp;&nbsp;&nbsp; -- look at the value of the variable<br>&nbsp;&nbsp;&nbsp; x &lt;- readIORef var<br>&nbsp;&nbsp;&nbsp; print x<br></font><br>You can scatter threadDelay throughout this code to mess with the scheduler and show how a failure could happen.<br><br>
Conversion to MVars should be pretty straightforward.<br><br>Luke<br><br><br><div class="EC_gmail_quote">2008/12/16 Mozhgan Kabiri <span dir="ltr">&lt;<a href="mailto:mozhgan_kch@hotmail.com">mozhgan_kch@hotmail.com</a>&gt;</span><br>
<blockquote class="EC_gmail_quote" style="padding-left: 1ex;">



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