Hi Eugene <br>Thank you for reply. <br><br><div class="gmail_quote">On Wed, Sep 5, 2012 at 12:32 AM, Eugene Perederey <span dir="ltr">&lt;<a href="mailto:eugene.perederey@gmail.com" target="_blank">eugene.perederey@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Why do you think main should block more than once?<br>
I see only two possible scenarios: the fun thread puts to mvar first<br>
thus blocking main,<br></blockquote><div><br>So at least in this case I should get thread blocked indefinitely in an MVar operation <br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

or 10 is put into mvar in main, blocking the other thread indefinitely.<br></blockquote><div> <br>or main will execute and fun thread will die. There are 50 - 50 chance for this ( assuming both are equally likely ). I did some modification in my code and Now I am  consistently getting &quot;Concurrent: thread blocked indefinitely in an MVar operation&quot;<br>
<br>import Data.List<br>import Control.Concurrent<br><br>fun m = do<br>   putMVar m 10<br>   return ()<br>   <br><br>main = do <br>  m &lt;- newEmptyMVar  <br>  forkIO $ fun m<br>  putStrLn &quot;I am inside main&quot;<br>
  putMVar m 10<br>  return ()<br><br>[mukesh.tiwari@ Programming]$ ghc-7.4.1 -threaded -fforce-recomp     Concurrent.hs <br>[1 of 1] Compiling Main             ( Concurrent.hs, Concurrent.o )<br>Linking Concurrent ...<br>
[mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>I am inside main<br>Concurrent: thread blocked indefinitely in an MVar operation<br>[mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>I am inside main<br>Concurrent: thread blocked indefinitely in an MVar operation<br>
[mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>I am inside main<br>Concurrent: thread blocked indefinitely in an MVar operation<br>[mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>I am inside main<br>Concurrent: thread blocked indefinitely in an MVar operation<br>
[mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>I am inside main<br>Concurrent: thread blocked indefinitely in an MVar operation<br> <br>My question is why the outcome in first case is deterministic. Every time the code executing  ( at least half the time main thread should be blocked ) . <br>
<br>Regards <br>Mukesh Tiwari <br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div><div class="h5"><br>
<br>
On 4 September 2012 11:54, mukesh tiwari &lt;<a href="mailto:mukeshtiwari.iiitm@gmail.com">mukeshtiwari.iiitm@gmail.com</a>&gt; wrote:<br>
&gt; Hello All<br>
&gt; I was going trough Real World Haskell and it says &quot;If we try to put a value<br>
&gt; into an MVar that is already full, our thread is put to sleep until another<br>
&gt; thread takes the value out&quot;. I wrote a simple code to block main<br>
&gt;<br>
&gt; import Data.List<br>
&gt; import Control.Concurrent<br>
&gt;<br>
&gt; fun m = do<br>
&gt;    putMVar m 10<br>
&gt;    return ()<br>
&gt;<br>
&gt;<br>
&gt; main = do<br>
&gt;   m &lt;- newEmptyMVar<br>
&gt;   forkIO $ fun m<br>
&gt;   putMVar m 10<br>
&gt;   return ()<br>
&gt;<br>
&gt; What I am expecting that main should be blocked at least couple of times<br>
&gt; but its behaving more deterministically.<br>
&gt; [mukesh.tiwari@ Programming]$ ghc-7.4.1 -threaded -fforce-recomp<br>
&gt; Concurrent.hs<br>
&gt; [1 of 1] Compiling Main             ( Concurrent.hs, Concurrent.o )<br>
&gt; Linking Concurrent ...<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent  +RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$<br>
&gt;<br>
&gt; I am expecting to get thread blocked indefinitely on MVar at least half the<br>
&gt; time. Could some one please tell me why this deterministic behavior ?<br>
&gt; Regards<br>
&gt; Mukesh Tiwari<br>
&gt;<br>
&gt;<br>
</div></div>&gt; _______________________________________________<br>
&gt; Beginners mailing list<br>
&gt; <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
&gt;<br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
Best,<br>
Eugene Perederey<br>
</font></span></blockquote></div><br>