[Haskell-cafe] threading question for ghc 7.0.2

oliver mueller oliver.mueller at gmail.com
Thu Mar 24 09:53:08 CET 2011


after reading in "GHC's implementation of concurrency" and even more
important in the documentation of hWaitForInput it seems clear that my
receiver thread blocks and the sender cannot be executed:
"NOTE for GHC users: unless you use the -threaded flag, hWaitForInput
t where t >= 0 will block all other Haskell threads for the duration
of the call. It behaves like a safe foreign call in this respect."

even though ghc seems to use a preemptive scheduling strategy, i tried
to apply a 'yield' to the receiving thread which worked out quite
nicely. now the sender thread gets a chance to run.
from the docs:
"The yield action allows (forces, in a co-operative multitasking
implementation) a context-switch to any other currently runnable
threads (if any), and is occasionally useful when implementing
concurrency abstractions."

still i'm not sure my code is the appropriate solution to the problem
i'm trying to solve. and strange to me that my initial solution seemed
to be working just fine on ghc 6.x.x.
if anybody knows of a better way please let me know!
code is again at https://gist.github.com/882770 (including the little
'yield' in line 74)

cheers,
oliver

On Mar 23, 11:47 am, Oliver Mueller <oliver.muel... at gmail.com> wrote:
> hello,
> i'm a bit lost here so i hope s.o. can help me here:
> seems i have a problem in my code when compiled with ghc-7.0.2:
>
> what i try to do:
> serverside:
> -listen on a socket and reply to requests
> client-side:
> -send a request to that server
> -wait for a response (with a timeout)
>
> any ideas on how i can make this work again without using the -threaded flag?
> is there is a better way for what i'm trying to achieve?
> many thanks,
> oliver
>
> the simplified code is available athttps://gist.github.com/882770
> explanation:
> the code forks of a lightweight thread (not an OS thread) that will
> listen for incoming responses.
> the main thread will go on to push out a message and then wait for the
> receiver thread to finish.
>
> i used to compile my application with the -threaded option but ran
> into some obscure FFI problems after upgrading from ghc-6.12.3 to
> ghc-7.0.2. so i tried to remove the -threaded option.
> but now the message does not get pushed out until the receiver-thread
> has finished.
> what is strange to me is that this worked without problems on 6.12.3
> but not on 7.0.2.
> also the behavior is a little different on mac os X with 7.0.2: here i
> get a "resource vanished (Connection reset by peer)"
>
> see the output of the program below ([S] marks output from the main
> thread, [R] from the receiver thread)
>
> compiled on ghc-7.0.2/ubuntu 64bit, without -threaded
>
> "running all things..."
>    [R]listening for response...
>    [R]wait for data with timeout:2000 ms
> ........................................................................... ........................................................................... ...................................................
>    [R]no message available
> [S]pushing out the message
> [S]sending --> "\EOT\ETX"
> "[S]going on..."
> Nothing
>
> compiled on ghc-7.0.2/ubuntu 64bit, using -threaded
>
> "running all things..."
> [S]pushing out the message
>    [R]listening for response...
> [S]sending --> "\EOT\ETX"
>    [R]wait for data with timeout:2000 ms
> "[S]going on..."
>
> ..................................................Just "\ETX\EOT"
>
> and...also very strange: when i introduce a putStrLn in the main
> thread (not a print !) it will work again...only a lot
> (see code snipped line38: putStrLn "[S]hickup"
>
> "running all things..."
> [S]hickup
>    [R]listening for response...
> [S]pushing out the message
> [S]sending --> "\EOT\ETX"
>    [R]wait for data with timeout:2000 ms
> "[S]going on..."
> ..................................................Just "\ETX\EOT"
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-C... at haskell.orghttp://www.haskell.org/mailman/listinfo/haskell-cafe



More information about the Haskell-Cafe mailing list