Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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)
  in
  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
ecc@cmu.edu
-------------------
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