English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Timeouts and event
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2001-09-13 (19:17)
From: Eric C. Cooper <ecc@c...>
Subject: Re: [Caml-list] Timeouts and event
Nicolas George wrote:
> Is it possible to do something like Event.select, but that would only block
> for a limited amount of time? It is possible to loop on Event.poll and
> Thread.delay, but that keeps the thread a bit busy.

You can do this nicely using a "watchdog event" that produces a value
after a timeout.

let watchdog seconds v =
  let channel = Event.new_channel () in
  let watchdog_thread () =
    Thread.delay seconds;
    Event.sync (Event.send channel v)
  ignore (Thread.create watchdog_thread ());
  Event.receive channel

Using the watchdog event, you can make a version of sync that returns a
known value in the case of a timeout:

let timed_sync timeout_interval timeout_val event =
  Event.sync (Event.choose [event;
			    watchdog timeout_interval timeout_val])

Or you can make a version that returns an option (None for a timeout,
Some x for a "real" sync):

let optional_sync timeout event =
  Event.sync (Event.choose [Event.wrap event (fun x -> Some x);
			    watchdog timeout None])

Eric Cooper
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr