Hoogle Search

Within LTS Haskell 24.68 (ghc-9.10.3)

Note that Stackage only displays results for the latest LTS and Nightly snapshot. Learn more.

  1. module Core.Program.Threads

    Utility functions for running Program actions concurrently. Haskell uses green threads: small lines of work that are scheduled down onto actual execution contexts (set by default by this library to be one per core). Haskell threads are incredibly lightweight, and you are encouraged to use them freely. Haskell provides a rich ecosystem of tools to do work concurrently and to communicate safely between threads. This module provides wrappers around some of these primatives so you can use them easily from the Program monad. Note that when you fire off a new thread the top-level application state is shared; it's the same τ inherited from the parent Program.

  2. data Thread α

    core-program Core.Program.Threads

    A thread for concurrent computation. (this wraps base's ThreadId along with a holder for the result of the thread)

  3. ThreadCancelled :: Terminator

    core-program Core.Program.Threads

    No documentation available.

  4. cancelThread :: Thread α -> Program τ ()

    core-program Core.Program.Threads

    Cancel a thread. Be careful when using this. If you are planning cancel a worker thread then the main thread that is waitThreading on it will throw an exception, specifically ThreadCancelled (unless something else has already thrown an exception in which case that will be thrown instead). In this scenario you will need to catch around your waiting function otherwise the uncaught exception will continue to unwind your execution stack and probably end your program. (this wraps base's killThread. The underlying mechanism used is to throw the ThreadKilled exception to the other thread. That exception is asynchronous, so will not be trapped by a catch block and will indeed cause the thread receiving the exception to come to an end)

  5. concurrentThreads :: Program τ α -> Program τ β -> Program τ (α, β)

    core-program Core.Program.Threads

    Fork two threads and wait for both to finish. The return value is the pair of each action's return types. This is the same as calling forkThread and waitThread twice, except that if either sub-program fails with an exception the other program which is still running will be cancelled and the original exception is then re-thrown.

    (a,b) <- concurrentThreads one two
    
    -- continue, doing something with both results.
    
    For a variant that ingores the return values and just waits for both see concurrentThreads_ below.

  6. concurrentThreads_ :: Program τ α -> Program τ β -> Program τ ()

    core-program Core.Program.Threads

    Fork two threads and wait for both to finish. This is the same as calling forkThread and waitThread_ twice, except that if either sub-program fails with an exception the other program which is still running will be cancelled and the original exception is then re-thrown.

  7. forkThread :: Program τ α -> Program τ (Thread α)

    core-program Core.Program.Threads

    Fork a thread. The child thread will run in the same Context as the calling Program, including sharing the user-defined application state value. If you want to find out what the result of a thread was use waitThread on the Thread object returned from this function. For example:

    t1 <- forkThread $ do
    info "Doing interesting stuff concurrently"
    pure True
    
    ...
    
    result <- waitThread t1
    
    if result
    then -- expected
    else -- not good
    
    If you don't need the result, you can use forkThread_ instead. Threads that are launched off as children are on their own! If the code in the child thread throws an exception that is not caught within that thread, the exception will kill the thread. Threads dying without telling anyone is a bit of an anti-pattern, so this library logs a warning-level log message if this happens. (this function wraps base's forkIO) Concerning telemetry Note that threads inherit the telemetry state from their parent. If you are using the tracing features from core-telemetry any telemetry registered in that side task will be included in the enclosing span active in the parent thread that spawned the thread:
    t2 <- forkThread $ do
    info "Performing quick side task"
    telemetry
    [ 'metric "counter" 42
    ]
    ...
    
    In this case the "counter" field in the parent thread's current span will get the value 42. This is appropriate for the common case where you are doing small side tasks concurrently to accelerate a larger computation. But at other times you are launching off a fully independent control flow and want it to have its own telemetry. In those cases, you'll want to start a new span (or even a new trace) immediately after forking the thread:
    forkThread_ $ do
    encloseSpan "subTask" $ do
    ...
    
    any telemetry from this worker thread will be appropriately nested in a new child span called "subTask".

  8. forkThread_ :: Program τ α -> Program τ ()

    core-program Core.Program.Threads

    Fork a thread with forkThread but do not wait for a result. This is on the assumption that the sub program will either be a side-effect and over quickly, or long-running daemon thread (presumably containing a forever loop in it), never returning.

  9. raceThreads :: Program τ α -> Program τ β -> Program τ (Either α β)

    core-program Core.Program.Threads

    Fork two threads and race them against each other. This blocks until one or the other of the threads finishes. The return value will be Left α if the first program (one) completes first, and Right β if it is the second program (two) which finishes first. The sub program which is still running will be cancelled with an exception.

    result <- raceThreads one two
    case result of
    Left a -> do
    -- one finished first
    Right b -> do
    -- two finished first
    
    For a variant that ingores the return value and just races the threads see raceThreads_ below.

  10. raceThreads_ :: Program τ α -> Program τ β -> Program τ ()

    core-program Core.Program.Threads

    Fork two threads and race them against each other. When one action completes the other will be cancelled with an exception. This is useful for enforcing timeouts:

    raceThreads_
    (sleepThread 300)
    (do
    -- We expect this to complete within 5 minutes.
    performAction
    )
    

Page 755 of many | Previous | Next