Skip to content

Why does threadDelay take Int

Summary

GHC.Conc.IO.threadDelay takes an Int argument. On 32-bit systems, the upper limit is 2147483647 us, which is less than 36 minutes. This is very inconvenient and leads to hidden bugs. Developers working on 64-bit machines may just call threadDelay with whatever duration they like. One day the program is compiled on a 32-bit machine and suddenly there are errors.

Steps to reproduce

Try to write a loop which runs a job every hour using threadDelay:

Version 1

import Control.Concurrent
import Control.Monad

runJob :: IO ()
runJob = print 1

main :: IO ()
main = forever $ runJob >> threadDelay 3600000000

Version 2

import Control.Concurrent
import Control.Monad

runJob :: IO ()
runJob = print 1

main :: IO ()
main = forever $ runJob >> threadDelay (3600 * 1000000)

Expected behavior

Both versions run the job every hour.

Actual behavior

On 32-bit systems, version 1 produces a warning when compiling. Version 2 compiles successfully but does not have any delay at all when running.

Environment

  • GHC version used: 7.10.3

Optional:

  • Operating System: Ubuntu 16.04
  • System Architecture: i686
Edited by Hypercube
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information