<div>Hi Guys, thanks again for the time reading this thread.</div><div>I was busy so I couldn&#39;t modify the code, but now it looks like this:</div><div><br></div><div><br></div><div>main = do </div><div>    finished &lt;- atomically $ newTChan </div>

<div>    chan &lt;- atomically $ newTChan </div><div>    f &lt;- openOffline &quot;filename&quot;</div><div>    forkIO $ writeFile chan</div><div>    forkIO $ reaFile f chan finished</div><div>    a &lt;- readTChan finished</div>

<div>    show a</div><div><br></div><div><br></div><div>So chan is the variable for communication and finished the variable used to readFile tell the main thread that the file is over,</div><div><br></div><div>It seems more neat than the previous code, </div>

<div>but I have a hard time understanding the sentence &quot;...<span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.499999046325684px">you have to manually specify the number of threads that </span><span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.499999046325684px">you opened...</span>&quot; in Felipe&#39;s email. What does it mean? How do I specify automatically how many threads do I want?</div>

<div><br></div><div><br></div><div>Thanks a lot</div><div><br></div><div>Mau :)</div><div><br></div><div><br></div><div><br></div><div><br></div><br><div class="gmail_quote">On Mon, Jul 2, 2012 at 10:18 PM, Bob Hutchison <span dir="ltr">&lt;<a href="mailto:hutch-lists@recursive.ca" target="_blank">hutch-lists@recursive.ca</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><div><div class="h5"><div>On 2012-07-02, at 9:10 AM, Bob Hutchison wrote:</div>

<br><blockquote type="cite"><div style="word-wrap:break-word"><br><div><div>On 2012-06-30, at 1:51 PM, Mauricio Hernandes wrote:</div><br><blockquote type="cite">Eternal Gratitude for the help, it&#39;s working perfectly, I will consider the exceptions and other stuff now.<div>

<br></div><div>the code looks like this now</div><div><br></div><div><br></div><div><div>import System.IO</div>

<div>import Control.Concurrent</div><div>import Data.List </div><div>import Control.Monad</div><div><br></div><div>main = do</div><div>          finished &lt;- newEmptyMVar</div><div>          input &lt;- newMVar  [1..30000]</div>



<div>          ia &lt;- newEmptyMVar</div><div>          ib &lt;- newEmptyMVar</div><div>          ic &lt;- newEmptyMVar</div><div><br></div><div>          forkIO $ do x &lt;- readMVar input</div><div>                      putMVar ia x</div>



<div>                      putMVar finished ()</div><div><br></div><div>          forkIO $ do a &lt;- readMVar ia</div><div>                      putMVar ib ( sum a )</div><div>                      putMVar finished ()</div>



<div><br></div><div>          forkIO $ do a &lt;- readMVar ia</div><div>                      putMVar ic ( reverse a )</div><div>                      putMVar finished ()</div><div><br></div><div>          b &lt;- readMVar ib</div>



<div>          c &lt;- readMVar ic</div><div>          writeFile &quot;somaEprod.txt&quot; (show b ++ &quot;\n&quot;) </div><div>          appendFile &quot;somaEprod.txt&quot; (show c)             </div><div>          replicateM_ 3 (takeMVar finished)</div>

</div></blockquote><div><br></div>Just another Haskell beginner here, so beware...</div><div><br></div><div>You&#39;ve moved the readMVar out of a thread into the application. This means (I think) that you are waiting for values in both ib and ic in the application (rather than a fourth thread). In your specific program, for these to have values require that all three threads have completed so you don&#39;t need the finished MVar anymore. However, this is pretty fragile being completely dependent on the MVars being set exactly once as the threads complete (so if you modify the code you have to be careful). The found solution is also fragile as </div>

</div></blockquote></div></div>                                                     ^^^^^ finished (sorry)<div><div class="h5"><br><blockquote type="cite"><div style="word-wrap:break-word"><div>Felipe says in his post. I don&#39;t know what those libraries Felipe mentioned are but I think I&#39;d be looking for them right about now if I were you :-)</div>

<div><br></div><div>Cheers,</div><div>Bob</div><div><br><blockquote type="cite"><div>

<div><br></div><div><br></div><div><br></div><div><br></div><div>Valeu</div><div>Mauricio</div><br><div class="gmail_quote">On Sun, Jul 1, 2012 at 12:24 AM, Felipe Almeida Lessa <span dir="ltr">&lt;<a href="mailto:felipe.lessa@gmail.com" target="_blank">felipe.lessa@gmail.com</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Your application is exiting before your forkIOs get a chance to<br>
execute.  Instead of<br>
<br>
  forkIO $ do<br>
    ...<br>
  forkIO $ do<br>
    ...<br>
  forkIO $ do<br>
    ...<br>
<br>
use something like<br>
<br>
  finished &lt;- newEmptyMVar<br>
<br>
  forkIO $ do<br>
    ...<br>
    putMVar finished ()<br>
<br>
  forkIO $ do<br>
    ...<br>
    putMVar finished ()<br>
<br>
  forkIO $ do<br>
    ...<br>
    putMVar finished ()<br>
<br>
  replicateM_ 3 (takeMVar finished)<br>
<br>
Doing so will avoid your program to exit until all threads have finished.<br>
<br>
Note that the code above is extremely fragile: doesn&#39;t handle<br>
exceptions, you have to manually specify the number of threads that<br>
you opened, etc.  These are abstracted by some libraries on Hackage<br>
that you may use later for Real World Code (TM).<br>
<br>
Cheers, =)<br>
<span><font color="#888888"><br>
--<br>
Felipe.<br>
</font></span></blockquote></div><br></div>
_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@haskell.org" target="_blank">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>

</blockquote></div><br><div>
<span style="border-collapse:separate;font-family:Verdana;font-size:medium;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"><span style="border-collapse:separate;font-family:Verdana;font-size:medium;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="word-wrap:break-word">

<span style="border-collapse:separate;font-family:Verdana;font-size:medium;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="word-wrap:break-word">

<span style="border-collapse:separate;font-family:Verdana;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="word-wrap:break-word">

<span style="border-collapse:separate;font-family:Verdana;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="word-wrap:break-word">

<div><div>----</div><div>Bob Hutchison</div><div>Recursive Design Inc.</div><div><a href="http://www.recursive.ca/" target="_blank">http://www.recursive.ca/</a></div><div>weblog: <span style="font-family:&#39;Lucida Grande&#39;"><a href="http://xampl.com/so" target="_blank">http://xampl.com/so</a></span></div>

</div></div></span></div></span></div></span><div><font face="&#39;Lucida Grande&#39;"><br></font></div></div></span><br></span><br>
</div>
<br></div>_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@haskell.org" target="_blank">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>

</blockquote></div></div></div><div><div class="h5"><br><div>
<span style="text-indent:0px;letter-spacing:normal;font-variant:normal;text-align:auto;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Verdana;word-spacing:0px"><span style="text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Verdana;word-spacing:0px"><div style="word-wrap:break-word">

<span style="text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:medium;white-space:normal;font-family:Verdana;word-spacing:0px"><div style="word-wrap:break-word">

<span style="text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:12px;white-space:normal;font-family:Verdana;word-spacing:0px"><div style="word-wrap:break-word">

<span style="text-indent:0px;letter-spacing:normal;font-variant:normal;font-style:normal;font-weight:normal;line-height:normal;border-collapse:separate;text-transform:none;font-size:12px;white-space:normal;font-family:Verdana;word-spacing:0px"><div style="word-wrap:break-word">

<div><div>----</div><div>Bob Hutchison</div><div>Recursive Design Inc.</div><div><a href="http://www.recursive.ca/" target="_blank">http://www.recursive.ca/</a></div><div>weblog: <span style="font-family:&#39;Lucida Grande&#39;"><a href="http://xampl.com/so" target="_blank">http://xampl.com/so</a></span></div>

</div></div></span></div></span></div></span><div><font face="&#39;Lucida Grande&#39;"><br></font></div></div></span><br></span><br>
</div>
<br></div></div></div></blockquote></div><br>