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 219585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:51:32+00:00 2026-05-11T18:51:32+00:00

how could one implement a function in concurrent haskell that either returns ‘a’ successfully

  • 0

how could one implement a function in concurrent haskell that either returns ‘a’ successfully or due to timeout ‘b’?

timed :: Int → IO a → b → IO (Either a b)
timed max act def = do

Best Regards,
Cetin Sert

Note: the signature of timed can be completely or slightly different.

  • 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-11T18:51:32+00:00Added an answer on May 11, 2026 at 6:51 pm

    Implementing your desired timed on top of System.Timeout.timeout is easy:

    import System.Timeout (timeout)
    
    timed :: Int -> IO a -> b -> IO (Either b a)
    timed us act def = liftM (maybe (Left def) Right) (timeout us act)
    

    By the way, the common implementation of timeout is closer to this: ($! = seq to try to force evaluation of the returned value in the thread rather than only returning a thunk):

    import Control.Concurrent (forkIO, threadDelay, killThread)
    import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
    import System.IO (hPrint, stderr)
    
    timeout :: Int -> IO a -> IO (Maybe a)
    timeout us act = do
        mvar <- newEmptyMVar
        tid1 <- forkIO $ (putMVar mvar . Just $!) =<< act
        tid2 <- forkIO $ threadDelay us >> putMVar mvar Nothing
        res <- takeMVar mvar
        killThread (maybe tid1 (const tid2) res) `catch` hPrint stderr
        return res
    

    The implementation of System.Timeout.timeout in the libraries is a little more complex, handling more exceptional cases.

    import Control.Concurrent  (forkIO, threadDelay, myThreadId, killThread)
    import Control.Exception   (Exception, handleJust, throwTo, bracket)
    import Data.Typeable
    import Data.Unique         (Unique, newUnique)
    
    data Timeout = Timeout Unique deriving Eq
    timeoutTc :: TyCon
    timeoutTc = mkTyCon "Timeout"
    instance Typeable Timeout where { typeOf _ = mkTyConApp timeoutTc [] }
    instance Show Timeout where
        show _ = "<<timeout>>"
    instance Exception Timeout
    
    timeout n f
        | n <  0    = fmap Just f
        | n == 0    = return Nothing
        | otherwise = do
            pid <- myThreadId
            ex  <- fmap Timeout newUnique
            handleJust (\e -> if e == ex then Just () else Nothing)
                       (\_ -> return Nothing)
                       (bracket (forkIO (threadDelay n >> throwTo pid ex))
                                (killThread)
                                (\_ -> fmap Just f))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You used a mgr.Dispose call in the second example. I'll… May 12, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Did you try to use the [STAThread] attribute in the… May 12, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer I figured it out. -keepattributes Exceptions May 12, 2026 at 12:40 am

Related Questions

how could one implement a function in concurrent haskell that either returns 'a' successfully
I've got a legacy application which is implemented in a number of Excel workbooks.
I'm trying to figure out how to implement a function in a tree node
I'm a mechanical engineering grad student and my adviser has just asked me to
In my web app I use several asmx (Web Services) from the same provider,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.