[Haskell-cafe] Threads with high CPU usage

Mads Lindstrøm mads_lindstroem at yahoo.dk
Sun Dec 21 17:35:06 EST 2008


Hi Peter,

Peter Verswyvelen wrote:
> Would forkOS instead of forkIO help in this case?

No, according to
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#v%3AforkOS :

"Using forkOS instead of forkIO makes no difference at all to the
scheduling behaviour of the Haskell runtime system. It is a common
misconception that you need to use forkOS instead of forkIO to avoid
blocking all the Haskell threads when making a foreign call; this isn't
the case. To allow foreign calls to be made without blocking all the
Haskell threads (with GHC), it is only necessary to use the -threaded
option when linking your program, and to make sure the foreign import is
not marked unsafe."

/Mads

> 
> On Sun, Dec 21, 2008 at 11:16 PM, Mads Lindstrøm
> <mads_lindstroem at yahoo.dk> wrote:
>         Hi Günter
>         
>         Günther Schmidt wrote:
>         > Hi,
>         >
>         > in an application of mine I start a long-running operation
>         in a thread via
>         > forkIO so that the UI process doesn't get blocked.
>         > It just so happens, that the long-running process also takes
>         the CPU to
>         > nearly 100% while it runs.
>         >
>         > During that time the run-time system does *not* switch back
>         and forth
>         > between the UI-process and the long-running task, it seems
>         that the UI
>         > process only gets woken up *after* the high CPU thread
>         finishes completely.
>         >
>         > To the effect of course that it makes no difference at all
>         to the UIs
>         > responsiveness whether I use forkIO or not.
>         >
>         > The long running process is pretty atomic, it's a single
>         query to the
>         > database which takes up to a minute to complete so I don't
>         see a chance to
>         > squeeze a "mainIteration" in there.
>         
>         
>         It could be the database library, as it may use unsafe foreign
>         calls.
>         Unsafe foreign calls blocks all other threads, even if you
>         compile with
>         the -threaded option. See
>         http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#4 .
>         
>         I cannot claim to know how all Haskell database libraries are
>         implemented, but at least some of them use unsafe foreign
>         calls. So
>         which database library is you using?
>         
>         >
>         > What can I do?
>         
>         If the problem has to do with unsafe foreign calls, then you
>         can
>         implement the database calls in a separate process. Not the
>         easiest
>         options, but I can think of no other.
>         
>         >
>         > Günther
>         >
>         
>         /Mads Lindstrøm
>         
>         
>         
>         
>         _______________________________________________
>         Haskell-Cafe mailing list
>         Haskell-Cafe at haskell.org
>         http://www.haskell.org/mailman/listinfo/haskell-cafe
>         
> 
> 




More information about the Haskell-Cafe mailing list