Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Concurrent vs GHC (Bob Hutchison)
2. Re: Concurrent vs GHC (Bob Hutchison)
----------------------------------------------------------------------
Message: 1
Date: Mon, 2 Jul 2012 09:10:46 -0400
From: Bob Hutchison <[email protected]>
Subject: Re: [Haskell-beginners] Concurrent vs GHC
To: [email protected]
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On 2012-06-30, at 1:51 PM, Mauricio Hernandes wrote:
> Eternal Gratitude for the help, it's working perfectly, I will consider the
> exceptions and other stuff now.
>
> the code looks like this now
>
>
> import System.IO
> import Control.Concurrent
> import Data.List
> import Control.Monad
>
> main = do
> finished <- newEmptyMVar
> input <- newMVar [1..30000]
> ia <- newEmptyMVar
> ib <- newEmptyMVar
> ic <- newEmptyMVar
>
> forkIO $ do x <- readMVar input
> putMVar ia x
> putMVar finished ()
>
> forkIO $ do a <- readMVar ia
> putMVar ib ( sum a )
> putMVar finished ()
>
> forkIO $ do a <- readMVar ia
> putMVar ic ( reverse a )
> putMVar finished ()
>
> b <- readMVar ib
> c <- readMVar ic
> writeFile "somaEprod.txt" (show b ++ "\n")
> appendFile "somaEprod.txt" (show c)
> replicateM_ 3 (takeMVar finished)
Just another Haskell beginner here, so beware...
You'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'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 Felipe says in his post. I don't know what those libraries Felipe mentioned
are but I think I'd be looking for them right about now if I were you :-)
Cheers,
Bob
>
>
>
>
> Valeu
> Mauricio
>
> On Sun, Jul 1, 2012 at 12:24 AM, Felipe Almeida Lessa
> <[email protected]> wrote:
> Your application is exiting before your forkIOs get a chance to
> execute. Instead of
>
> forkIO $ do
> ...
> forkIO $ do
> ...
> forkIO $ do
> ...
>
> use something like
>
> finished <- newEmptyMVar
>
> forkIO $ do
> ...
> putMVar finished ()
>
> forkIO $ do
> ...
> putMVar finished ()
>
> forkIO $ do
> ...
> putMVar finished ()
>
> replicateM_ 3 (takeMVar finished)
>
> Doing so will avoid your program to exit until all threads have finished.
>
> Note that the code above is extremely fragile: doesn't handle
> exceptions, you have to manually specify the number of threads that
> you opened, etc. These are abstracted by some libraries on Hackage
> that you may use later for Real World Code (TM).
>
> Cheers, =)
>
> --
> Felipe.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120702/dd929666/attachment-0001.htm>
------------------------------
Message: 2
Date: Mon, 2 Jul 2012 09:18:25 -0400
From: Bob Hutchison <[email protected]>
Subject: Re: [Haskell-beginners] Concurrent vs GHC
To: Bob Hutchison <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
On 2012-07-02, at 9:10 AM, Bob Hutchison wrote:
>
> On 2012-06-30, at 1:51 PM, Mauricio Hernandes wrote:
>
>> Eternal Gratitude for the help, it's working perfectly, I will consider the
>> exceptions and other stuff now.
>>
>> the code looks like this now
>>
>>
>> import System.IO
>> import Control.Concurrent
>> import Data.List
>> import Control.Monad
>>
>> main = do
>> finished <- newEmptyMVar
>> input <- newMVar [1..30000]
>> ia <- newEmptyMVar
>> ib <- newEmptyMVar
>> ic <- newEmptyMVar
>>
>> forkIO $ do x <- readMVar input
>> putMVar ia x
>> putMVar finished ()
>>
>> forkIO $ do a <- readMVar ia
>> putMVar ib ( sum a )
>> putMVar finished ()
>>
>> forkIO $ do a <- readMVar ia
>> putMVar ic ( reverse a )
>> putMVar finished ()
>>
>> b <- readMVar ib
>> c <- readMVar ic
>> writeFile "somaEprod.txt" (show b ++ "\n")
>> appendFile "somaEprod.txt" (show c)
>> replicateM_ 3 (takeMVar finished)
>
> Just another Haskell beginner here, so beware...
>
> You'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'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
^^^^^ finished (sorry)
> Felipe says in his post. I don't know what those libraries Felipe mentioned
> are but I think I'd be looking for them right about now if I were you :-)
>
> Cheers,
> Bob
>
>>
>>
>>
>>
>> Valeu
>> Mauricio
>>
>> On Sun, Jul 1, 2012 at 12:24 AM, Felipe Almeida Lessa
>> <[email protected]> wrote:
>> Your application is exiting before your forkIOs get a chance to
>> execute. Instead of
>>
>> forkIO $ do
>> ...
>> forkIO $ do
>> ...
>> forkIO $ do
>> ...
>>
>> use something like
>>
>> finished <- newEmptyMVar
>>
>> forkIO $ do
>> ...
>> putMVar finished ()
>>
>> forkIO $ do
>> ...
>> putMVar finished ()
>>
>> forkIO $ do
>> ...
>> putMVar finished ()
>>
>> replicateM_ 3 (takeMVar finished)
>>
>> Doing so will avoid your program to exit until all threads have finished.
>>
>> Note that the code above is extremely fragile: doesn't handle
>> exceptions, you have to manually specify the number of threads that
>> you opened, etc. These are abstracted by some libraries on Hackage
>> that you may use later for Real World Code (TM).
>>
>> Cheers, =)
>>
>> --
>> Felipe.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
> ----
> Bob Hutchison
> Recursive Design Inc.
> http://www.recursive.ca/
> weblog: http://xampl.com/so
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120702/a414c9cc/attachment.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 49, Issue 3
****************************************