Version française
Home     About     Download     Resources     Contact us    
Browse thread
Ask-if-continue wrapper?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jonathan Roewen <jonathan.roewen@g...>
Subject: Re: [Caml-list] Ask-if-continue wrapper?
Sounds like you'd need continuations to do it nicely/properly. I have
no idea if it's possible to support continuations in ocaml. But why
not use a couple of threads?

Something like:

let continueq f args timeout timeout_val =
  let result = ref timeout_val in
  let on_done = ref (fun () -> ()) in (* hack cause I dunno how to get
around the order of declarations *)
  let do_computation () = result := f args; !on_done () in
  let comp_t = Thread.create do_computation () in
  let rec do_timeout t =
    if t <= 0 then
      Thread.kill comp_t
    else begin
      Thread.delay (float_of_int t);
      print_string "set timeout: ";
      do_timeout (read_int ());
    end in
  let timeout_t = Thread.create do_timeout timeout in
  on_done := (fun () -> Thread.kill timeout_t);
  Thread.join timeout_t;
  !result;;

Very ugly, but should do what you want. I have to add that one of the
thread implementations for ocaml doesn't implement Thread.kill -- I
can't remember which -- which would break this horribly :-|

Jonathan

On 12/28/05, Stephen Brackin <stephen.brackin@verizon.net> wrote:
>
>
> I'd like an OCaml function, which I'll call continueq, with the property
> that for any function f with argument(s) fargs,
>
> continueq f fargs tsecs defaultval
>
> starts evaluating f on fargs and lets this evaluation proceed for up to
> tsecs seconds. If the computation of (f fargs) completes in this time, then
> it returns the result of that computation. Otherwise, it asks the user how
> many seconds to let the computation of (f fargs) proceed. If the user inputs
> a value less than or equal to 0, then it returns defaultval. If the user
> inputs a value tsecs' greater than 0, then it evaluates
>
> continueq f' fargs' tsecs' defaultval
>
> where (f' fargs') denotes the computation state of (f fargs) at the time it
> was interrupted.
>
> I want to give the user the option of continuing without having to repeat
> earlier calculations. This is similar to the checkpoint utility ckpt, and
> similar to the "timeout" function described in the O'Reilly "'Developing
> Applications with Objective Caml" reference book, but not quite the same as
> either. Will someone please tell me how to do it?
>
> Steve
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list:
> http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
>