Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6468851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:55:42+00:00 2026-05-25T05:55:42+00:00

Suppose I have a function f which takes an integer argument. f may not

  • 0

Suppose I have a function f which takes an integer argument. f may not terminate on some arguments, but its result is equally valuable. (For concreteness, the argument could be the seed to a random number generator, which is passed to a SAT solver.)

I want to use concurrency and invoke f 1, f 2, f 3, etc., and return when the first one finishes. So, each thread should be running code that looks like

comp <- start_proc (f 1)
wait(comp || anyDone) -- wait for _either_ of these signals to be true
if comp then
    set anyDone = True

What’s the easiest way to do this? The AMB operator comes to mind, but I’d need to run all processes simultaneously (e.g. on a 24- or 80-core machine). (Distributed computing solutions would be even better.) A superficial look at the AMB wiki page suggests it may not support non-terminating processes?

test

Currently, I’m not getting the answers to work with what I want. I think this is probably more of an issue with how I’m creating processes than anything else.

Define

runProc (x:xs) =
    createProcess (proc x xs) >>= \(_, _, _, h) -> waitForProcess h

Then, I want to race runProc ["zsh", "-c", "sleep 3"] and runProc ["ls"]. I modified Thomas’ answer a little, but it didn’t work.

raceL :: [IO α] -> IO α
raceL ops = do
    mv <- newEmptyMVar
    tids <- forM ops (\op -> forkIO (op >>= putMVar mv))
    answer <- takeMVar mv
    mapM_ killThread tids
    return answer

Compiling with -threaded and running with +RTS -N (I have a 4-core machine) doesn’t seem to help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:55:42+00:00Added an answer on May 25, 2026 at 5:55 am

    Instead of amb, consider unamb! It provides a handful of nice primitives for racing computations, both pure and impure. For example:

    Prelude Data.Unamb> unamb (last [1..]) 32
    32
    Prelude Data.Unamb> race (threadDelay 5000000 >> return 3) readLn
    Prelude Data.Unamb Control.Concurrent> race (threadDelay 5000000 >> return 3) readLn
    56
    56
    Prelude Data.Unamb Control.Concurrent> race (threadDelay 5000000 >> return 3) readLn
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which takes variadic arguments ( ... ) or a
Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)>
Suppose I have a function which takes a nullary functor as an argument: void
Suppose you have a function 'normalize' which takes a list of numbers (representing a
Suppose I have a simple program which takes argument input in one of the
I want to have a SelectAll function which takes in a few arguments (class,
Suppose I have a function which consults some external stateful service and returns a
Suppose I have a function which can either take an iterable/iterator or a non-iterable
Suppose I have a function which looks like this: template <class In, class In2>
Suppose I have an array of values [a,b,c,d,...] and a function f(x,...) which returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.