<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><blockquote type="cite"><div><br>From: Quentin Moser &lt;<a href="mailto:quentin.moser@unifr.ch">quentin.moser@unifr.ch</a>><br>Subject: Re: [Haskell-beginners] STM and IO<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't have time to<br>run.</div></blockquote><div><br></div><div>Doh!&nbsp;</div><div>I've been bitten before...</div><br><blockquote type="cite"><div>Now onto the second problem: ignore me if I'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>yes you are right of course</div><div><br></div><blockquote type="cite"><div>Note: I haven't tried running any of this code, but it seems simple<br>enough to be confident in.<br></div></blockquote><br></div><div>I had to make a change to myFork to get it to compile, here'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>import Control.Exception (finally)</div><div><br></div><div>myFork :: IO () -> IO (MVar ())</div><div>myFork a = do&nbsp;</div><div>&nbsp;&nbsp;v &lt;- newEmptyMVar</div><div>&nbsp;&nbsp;a `finally` (putMVar v ())</div><div>&nbsp;&nbsp;return v -- to honor the return&nbsp;type</div><div><br></div><div>myWait :: MVar () -> IO ()</div><div>myWait = readMVar&nbsp;</div><div><br></div><div>main = do</div><div>&nbsp;&nbsp; let fname = "store.txt"</div><div>&nbsp;&nbsp; fh &lt;- openFile fname ReadWriteMode</div><div>&nbsp;&nbsp; store &lt;- atomically $ newTMVar fh</div><div>&nbsp;&nbsp; waitMes &lt;- 10 `replicateM` (myFork $ writeTo store)</div><div>&nbsp;&nbsp; mapM_ myWait waitMes</div><div><br></div><div>writeTo :: TMVar (Handle) -> IO ()</div><div>writeTo store = do</div><div>&nbsp;&nbsp; fh &lt;- atomically $ takeTMVar store</div><div>&nbsp;&nbsp; text &lt;- hGetContents fh</div><div>&nbsp;&nbsp; hPutStr fh (text ++ " some text ")</div><div>&nbsp;&nbsp; atomically $ putTMVar store fh</div></div><div><br></div><div>Now I get the following error :&nbsp;test3: store.txt: hPutStr: illegal operation (handle is closed)</div><div><br></div><div><div>reading the doc about hGetContents, I found that &nbsp;: "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"</div><div><br></div><div><br></div><div>Intuitively I'd want to write something like :&nbsp;writeTo&nbsp;filename&nbsp;=&nbsp;atomically (do { s &lt;- readFile filename ; writeFile&nbsp;filename (s ++ "blah")&nbsp;})</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...&nbsp;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 class="Apple-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; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-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; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><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/">http://www.cimex.com</a></div></div></span></div></span> </div><br></body></html>
<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 postmaster@cimex.com

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.