Matthew Piziak
Matthew Piziak

Reputation: 3520

Sleeping less than a second in OCaml

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

Upvotes: 15

Views: 7906

Answers (3)

Valentyn Zakharenko
Valentyn Zakharenko

Reputation: 3088

from Unix module

val sleepf : float -> unit

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

Upvotes: 16

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66823

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

let minisleep (sec: float) =
    ignore (Unix.select [] [] [] sec)

Upvotes: 14

Matthew Piziak
Matthew Piziak

Reputation: 3520

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.

Upvotes: 15

Related Questions