14

The Unix.sleep function can suspend the program for whole seconds, but how can you suspend it for less than a second?

Matthew Piziak
  • 3,430
  • 4
  • 35
  • 49

3 Answers3

14

The classical Unix solution for this is to use select() with no file descriptors:

let minisleep (sec: float) =
    ignore (Unix.select [] [] [] sec)
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
13

The Thread.delay function pauses the thread for the given number of seconds, but it takes a float, allowing you to pause the thread for less than a second.

Matthew Piziak
  • 3,430
  • 4
  • 35
  • 49
13

from Unix module

val sleepf : float -> unit

Stop execution for the given number of seconds. Like sleep, but fractions of seconds are supported.

Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47
  • 2
    Note that this function was added in 4.03.0. Prior versions of OCaml don't have access to this particular function. – hcarty Aug 27 '16 at 21:09