<div dir="auto">It's pointing out that it's possible that after it takes the MVar some other thread could put the MVar. That could cause it to block, or could violate assumptions about how the MVar value changes. The basic rule is that whenever you're using an MVar you need all the threads to agree to some particular discipline. Most commonly, and best supported by the API, is "take, then put". However, there are other patterns, the most important of which is probably "put once", used to communicate thread completion.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jul 18, 2021, 9:49 AM Harendra Kumar <<a href="mailto:harendra.kumar@gmail.com">harendra.kumar@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>The documentation of withMVar (in Control.Concurrent.MVar module of base package) says:</div><div><br></div><div><pre><span>{-|
<a name="m_-4747692467819222987_line-175" rel="noreferrer"></a>  'withMVar' is an exception-safe wrapper for operating on the contents
<a name="m_-4747692467819222987_line-176" rel="noreferrer"></a>  of an 'MVar'.  This operation is exception-safe: it will replace the
<a name="m_-4747692467819222987_line-177" rel="noreferrer"></a>  original contents of the 'MVar' if an exception is raised (see
<a name="m_-4747692467819222987_line-178" rel="noreferrer"></a>  "Control.Exception").  However, it is only atomic if there are no
<a name="m_-4747692467819222987_line-179" rel="noreferrer"></a>  other producers for this 'MVar'.
<a name="m_-4747692467819222987_line-180" rel="noreferrer"></a>-}</span>
<a name="m_-4747692467819222987_line-181" rel="noreferrer"></a><a name="m_-4747692467819222987_line-184" rel="noreferrer"></a><span>withMVar</span> <span>::</span> <span>MVar</span> <span>a</span> <span>-></span> <span>(</span><span>a</span> <span>-></span> <span>IO</span> <span>b</span><span>)</span> <span>-></span> <span>IO</span> <span>b</span>
<a name="m_-4747692467819222987_line-185" rel="noreferrer"></a><span>withMVar</span> <span>m</span> <span>io</span> <span>=</span>
<a name="m_-4747692467819222987_line-186" rel="noreferrer"></a>  <span>mask</span> <span>$</span> <span>\</span><span>restore</span> <span>-></span> <span>do</span>
<a name="m_-4747692467819222987_line-187" rel="noreferrer"></a>    <span>a</span> <span><-</span> <span>takeMVar</span> <span>m</span>
<a name="m_-4747692467819222987_line-188" rel="noreferrer"></a>    <span>b</span> <span><-</span> <span>restore</span> <span>(</span><span>io</span> <span>a</span><span>)</span> <span>`onException`</span> <span>putMVar</span> <span>m</span> <span>a</span>
<a name="m_-4747692467819222987_line-189" rel="noreferrer"></a>    <span>putMVar</span> <span>m</span> <span>a</span>
<a name="m_-4747692467819222987_line-190" rel="noreferrer"></a>    <span>return</span> <span>b<br><br></span></pre><pre><span style="font-family:arial,sans-serif"><span>Can someone shed some light on what is meant by the statement - <br>"<span>However, it is only atomic if there are no other producers for this 'MVar'."?<br></span></span></span></pre><pre><span style="font-family:arial,sans-serif"><span><span>I hope this is the right mailing list for this question.<br></span></span></span></pre><pre><span style="font-family:arial,sans-serif"><span><span>Thanks,</span></span></span><span><span><span style="font-family:arial,sans-serif"><br>Harendra</span><br></span></span></pre></div></div>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>