Since the file handle is closed after you read the contents, you need to open a new one before writing to it.<br><br><div class="gmail_quote">On Thu, Apr 9, 2009 at 12:20 PM, emmanuel.delaborde <span dir="ltr">&lt;<a href="mailto:emmanuel.delaborde@cimex.com">emmanuel.delaborde@cimex.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style=""><div><blockquote type="cite"><div><br>From: Quentin Moser &lt;<a href="mailto:quentin.moser@unifr.ch" target="_blank">quentin.moser@unifr.ch</a>&gt;<br>
Subject: Re: [Haskell-beginners] STM and IO<div class="im"><br><br>Your problem has nothing to do with lazyness; Haskell simply kills all<br>other threads when the main thread returns from main. You have to<br>somehow wait for them to complete in main or they won&#39;t have time to<br>
run.</div></div></blockquote><div><br></div><div>Doh! </div><div>I&#39;ve been bitten before...</div><div class="im"><br><blockquote type="cite"><div>Now onto the second problem: ignore me if I&#39;m wrong, but it seems your<br>
intent is to spawn 10 threads that will each try to run (writeTo store)<br>once. What your current code does is spawn one thread that sequentially<br>runs writeTo 10 times.</div></blockquote><div><br></div></div><div>yes you are right of course</div>
<div class="im"><div><br></div><blockquote type="cite"><div>Note: I haven&#39;t tried running any of this code, but it seems simple<br>enough to be confident in.<br></div></blockquote><br></div></div><div>I had to make a change to myFork to get it to compile, here&#39;s what I have now:</div>
<div><br></div><div><div>module Main where</div><div><br></div><div>import Control.Monad</div><div>import Control.Concurrent</div><div>import Control.Concurrent.STM</div><div>import System.IO</div><div class="im"><div>import Control.Exception (finally)</div>
<div><br></div><div>myFork :: IO () -&gt; IO (MVar ())</div><div>myFork a = do </div><div>  v &lt;- newEmptyMVar</div><div>  a `finally` (putMVar v ())</div></div><div>  return v -- to honor the return type</div><div class="im">
<div><br></div><div>myWait :: MVar () -&gt; IO ()</div><div>myWait = readMVar </div><div><br></div></div><div class="im"><div>main = do</div><div>   let fname = &quot;store.txt&quot;</div><div>   fh &lt;- openFile fname ReadWriteMode</div>
<div>   store &lt;- atomically $ newTMVar fh</div><div>   waitMes &lt;- 10 `replicateM` (myFork $ writeTo store)</div><div>   mapM_ myWait waitMes</div><div><br></div></div><div class="im"><div>writeTo :: TMVar (Handle) -&gt; IO ()</div>
<div>writeTo store = do</div><div>   fh &lt;- atomically $ takeTMVar store</div><div>   text &lt;- hGetContents fh</div><div>   hPutStr fh (text ++ &quot; some text &quot;)</div><div>   atomically $ putTMVar store fh</div>
</div></div><div><br></div><div>Now I get the following error : test3: store.txt: hPutStr: illegal operation (handle is closed)</div><div><br></div><div><div>reading the doc about hGetContents, I found that  : &quot;Computation hGetContents hdl returns the list of characters corresponding to the unread portion of the channel or file managed by hdl, which is put into an intermediate state, semi-closed. In this state, hdl is effectively closed&quot;</div>
<div><br></div><div><br></div><div>Intuitively I&#39;d want to write something like : writeTo filename = atomically (do { s &lt;- readFile filename ; writeFile filename (s ++ &quot;blah&quot;) })</div><div>but the type system prevents me from doing IO within STM</div>
<div>I do not know how to go about sharing access to a file between multiple threads using STM... any pointers ?</div><div><br></div><div>Thanks</div><div><br></div><div>E.</div><div><br></div><div><br></div><div><br></div>
<div><br></div><div><br></div><div><br></div></div><div><br></div><div><br></div><br><div> <span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="">
<span style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div style="">
<div>--</div><div>Emmanuel Delaborde</div><div>Web Technologist</div><div>Cimex<br>53-55 Scrutton Street, London UK, EC2A 4PJ<br>T: +44 (0)20 7324 7780<br>F: +44 (0)20 7324 7781<br><a href="http://www.cimex.com/" target="_blank">http://www.cimex.com</a></div>
</div></span></div></span> </div><br></div><div><div></div><div class="h5">
<pre>-----------------------------------------------------------------------------------------------

This e-mail (and any attachments) is confidential and may contain 
personal views which are not the views of Cimex Media Ltd and 
any affiliated companies, unless specifically stated. It is intended 
for the use of the individual or group to whom it is addressed. If 
you have received it in error, please delete it from your system, 
do not use, copy or disclose the information in any way nor act in 
reliance on it and please notify <a href="mailto:postmaster@cimex.com" target="_blank">postmaster@cimex.com</a>

A company registered in England  Wales. Company Number 03765711
Registered Office : The Olde Bakehouse, 156 Watling Street East, Towcester,
Northants NN12 6DB

This email was scanned by Postini, the leading provider in Managed Email Security.

</pre></div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br>