Here's a small function I use, taken from the book "Developing Applications with Objective Caml" http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora168.html exception Timeout let sigalrm_handler = Sys.Signal_handle (fun _ -> raise Timeout) let timeout (time : int) (f : 'a -> 'b) (arg : 'a) = let old_behavior = Sys.signal Sys.sigalrm sigalrm_handler in let reset_sigalrm () = ignore (Unix.alarm 0); Sys.set_signal Sys.sigalrm old_behavior in ignore (Unix.alarm time) ; let res = f arg in reset_sigalrm () ; res On Mon, Apr 20, 2015 at 8:18 PM, Rodolphe Lepigre < rodolphe.lepigre@univ-savoie.fr> wrote: > I was wondering: is there a standard way to stop a computation after, say, > a given number of milliseconds (or seconds) in OCaml? > > For instance I would like to have a function > > exception Timeout > val exec : int -> ('a -> 'b) -> 'a -> 'b > > such that [exec t f x] computes [f x] but raises [Timeout] in case the > computation did not end before [t] milliseconds (or seconds). > > My guess would be that I need to use some Unix signals magic. Has anyone > come up with a clean solution to this problem? > > Thanks! > > Rodolphe > -- > Rodolphe Lepigre > LAMA, Université de Savoie, FRANCE > http://lama.univ-savoie.fr/~lepigre/ > > -- > Caml-list mailing list. Subscription management and archives: > https://sympa.inria.fr/sympa/arc/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs >