<div>Ah yes, then you&#39;re doing a double handshaking in a sense, of course. Not ideal if you just want to put something in the MVar and resume as quick as possible. However, in that case one could fork a dummy thread that only does readMVar I guess.</div>
<div><br></div><div>But does it also work when you want multiple threads that are waiting for the same MVar, and a single thread that writes to the MVar? It think is does, when you use readMVar one thread will get the value and the others will get it sequentially since readMVar puts the value back...&nbsp;</div>
<div><br></div><div>Again you guys gave a solution, an amazing brain thank this cafe is... Usually one gets drunk in a cafe, destroying braincells. Here it&#39;s the opposite :)</div><br><div class="gmail_quote">On Fri, Feb 13, 2009 at 11:01 AM, Duncan Coutts <span dir="ltr">&lt;<a href="mailto:duncan.coutts@worc.ox.ac.uk">duncan.coutts@worc.ox.ac.uk</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="Wj3C7c">On Thu, 2009-02-12 at 22:58 -0500, David Menendez wrote:<br>
&gt; On Thu, Feb 12, 2009 at 6:26 PM, Don Stewart &lt;<a href="mailto:dons@galois.com">dons@galois.com</a>&gt; wrote:<br>
&gt; &gt; bugfact:<br>
&gt; &gt;&gt; Consider the following code<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; stamp v x = do<br>
&gt; &gt;&gt; &nbsp; t &lt;- getCurrentTime<br>
&gt; &gt;&gt; &nbsp; putMVar v (x,t)<br>
&gt; &gt;&gt;<br>
&gt; &gt;&gt; Is it possible - with GHC - that a thread switch happens after the t &lt;-<br>
&gt; &gt;&gt; getCurrentTime and the putMVar v (x,t)?<br>
&gt; &gt;<br>
&gt; &gt; Yes. if &#39;t&#39; is heap allocated, there could be a context switch.<br>
&gt; &gt;<br>
&gt; &gt;&gt; If so, how would it be possible to make sure that the operation of reading the<br>
&gt; &gt;&gt; current time and writing the pair to the MVar is an &quot;atomic&quot; operation, in the<br>
&gt; &gt;&gt; sense that no thread switch can happen between the two? Would this require STM?<br>
&gt; &gt;&gt;<br>
&gt; &gt;<br>
&gt; &gt; Using &#39;atomically&#39; and TVars in STM, perhaps? Else, use withMVar? &nbsp; Or a<br>
&gt; &gt; modifyMVar in IO?<br>
&gt;<br>
&gt; As I understand it, withMVar or modifyMVar will protect you from<br>
&gt; extraneous exceptions, but they won&#39;t prevent another thread from<br>
&gt; writing to the MVar between the take and the put.<br>
<br>
</div></div>You have to cooperate with the other users of the MVar.<br>
<br>
If each thread is using readMVar, withMVar or modifyMVar then it&#39;s fine.<br>
The read/with/modify actions do a takeMVar followed by a putMVar. If one<br>
thread is using withMVar and another is doing putMVar directly then the<br>
exclusion scheme does not work.<br>
<font color="#888888"><br>
Duncan<br>
<br>
</font></blockquote></div><br>