<!doctype html public "-//W3C//DTD W3 HTML//EN">
<html><head><style type="text/css"><!--
blockquote, dl, ul, ol, li { padding-top: 0 ; padding-bottom: 0 }
 --></style><title>Re: [Haskell-beginners] Thread
Blocking</title></head><body>
<div>There's no basis on which to assume both alternative schedules
are equally likely.&nbsp; I would guess that, in your first example,
there's enough start-up cost for a new thread that the main thread
consistently gets to its putMVar first, whereas in the second example,
main is waylaid with its putStrLn, allowing the forked thread to get
there first consistently.</div>
<div><br></div>
<div>Dean</div>
<div><br></div>
<div>At 12:46 AM +0530 9/5/12, mukesh tiwari wrote:</div>
<blockquote type="cite" cite>Hi Eugene<br>
Thank you for reply.<br>
</blockquote>
<blockquote type="cite" cite>On Wed, Sep 5, 2012 at 12:32 AM, Eugene
Perederey &lt;<a
href="mailto:eugene.perederey@gmail.com">eugene.perederey@gmail.com</a
>&gt; wrote:<br>
<blockquote>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>
</blockquote>
<blockquote type="cite" cite><br>
So at least in this case I should get thread blocked indefinitely in
an MVar operation<br>
&nbsp;<br>
<blockquote>or 10 is put into mvar in main, blocking the other thread
indefinitely.<br>
</blockquote>
</blockquote>
<blockquote type="cite" cite>&nbsp;<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&nbsp; 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>
&nbsp;&nbsp; putMVar m 10<br>
&nbsp;&nbsp; return ()<br>
&nbsp;<br>
<br>
main = do<br>
&nbsp; m &lt;- newEmptyMVar&nbsp;<br>
&nbsp; forkIO $ fun m<br>
&nbsp; putStrLn &quot;I am inside main&quot;<br>
&nbsp; putMVar m 10<br>
&nbsp; return ()<br>
<br>
[mukesh.tiwari@ Programming]$ ghc-7.4.1 -threaded
-fforce-recomp&nbsp;&nbsp;&nbsp;&nbsp; Concurrent.hs<br>
[1 of 1] Compiling
Main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span
></span>&nbsp;&nbsp; ( Concurrent.hs, Concurrent.o )<br>
Linking Concurrent ...<br>
[mukesh.tiwari@ Programming]$ ./Concurrent&nbsp; +RTS -N2<br>
I am inside main<br>
Concurrent: thread blocked indefinitely in an MVar operation<br>
[mukesh.tiwari@ Programming]$ ./Concurrent&nbsp; +RTS -N2<br>
I am inside main<br>
Concurrent: thread blocked indefinitely in an MVar operation<br>
[mukesh.tiwari@ Programming]$ ./Concurrent&nbsp; +RTS -N2<br>
I am inside main<br>
Concurrent: thread blocked indefinitely in an MVar operation<br>
[mukesh.tiwari@ Programming]$ ./Concurrent&nbsp; +RTS -N2<br>
I am inside main<br>
Concurrent: thread blocked indefinitely in an MVar operation<br>
[mukesh.tiwari@ Programming]$ ./Concurrent&nbsp; +RTS -N2<br>
I am inside main<br>
Concurrent: thread blocked indefinitely in an MVar operation<br>
&nbsp;<br>
My question is why the outcome in first case is deterministic. Every
time the code executing&nbsp; ( at least half the time main thread
should be blocked ) .<br>
<br>
Regards<br>
Mukesh Tiwari<br>
<blockquote><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;&nbsp;&nbsp; &nbsp;putMVar m 10<br>
&gt;&nbsp;&nbsp; &nbsp;return ()<br>
&gt;<br>
&gt;<br>
&gt; main = do<br>
&gt;&nbsp;&nbsp; m &lt;- newEmptyMVar<br>
&gt;&nbsp;&nbsp; forkIO $ fun m<br>
&gt;&nbsp;&nbsp; putMVar m 10<br>
&gt;&nbsp;&nbsp; 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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span
></span>&nbsp;&nbsp; ( Concurrent.hs, Concurrent.o )<br>
&gt; Linking Concurrent ...<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS -N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS
-N2</blockquote>
<blockquote>&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+RTS
-N2<br>
&gt; [mukesh.tiwari@ Programming]$ ./Concurrent &nbsp;+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>
</blockquote>
<blockquote>&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"
>http://www.haskell.org/mailman/listinfo/beginners</a><br>
&gt;<br>
<font color="#888888"><br>
<br>
<br>
--<br>
Best,<br>
Eugene Perederey</font><br>
</blockquote>
</blockquote>
<blockquote type="cite" cite><br></blockquote>
<blockquote type="cite" cite><br>
_______________________________________________<br>
Beginners mailing list<br>
Beginners@haskell.org<br>
http://www.haskell.org/mailman/listinfo/beginners</blockquote>
<div><br></div>
</body>
</html>