caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Timeouts and event
@ 2001-09-13  9:35 Nicolas George
  2001-09-13 19:16 ` Eric C. Cooper
  0 siblings, 1 reply; 3+ messages in thread
From: Nicolas George @ 2001-09-13  9:35 UTC (permalink / raw)
  To: caml-list

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.
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Timeouts and event
  2001-09-13  9:35 [Caml-list] Timeouts and event Nicolas George
@ 2001-09-13 19:16 ` Eric C. Cooper
  2001-09-13 22:04   ` Nicolas George
  0 siblings, 1 reply; 3+ messages in thread
From: Eric C. Cooper @ 2001-09-13 19:16 UTC (permalink / raw)
  To: Nicolas George; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Timeouts and event
  2001-09-13 19:16 ` Eric C. Cooper
@ 2001-09-13 22:04   ` Nicolas George
  0 siblings, 0 replies; 3+ messages in thread
From: Nicolas George @ 2001-09-13 22:04 UTC (permalink / raw)
  To: caml-list

Le jeudi 13 septembre 2001 à 15:16, Eric C. Cooper a écrit :
> You can do this nicely using a "watchdog event" that produces a value
> after a timeout.

Ok, you create a new thread, who will only send the event after the
timeout. I am always a bit reluctant in creating new threads, especially if
I use system threads. If there were some builtin timeout event, it would be
great. And since we are in feature wish, event corresponding to wait_read
and wait_write.

> 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

I would write the two last lines like that:

let child = Thread.create watchdog_thread () in
let event = Event.receive channel in
Event.wrap_abort event ~f:(fun () -> Thread.kill child)

Aargh, Thread.kill does not exist with system threads!
(I see one solution, which is to start a new thread that will only sync the
event, but I think it is a waste.)
-------------------
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-09-13 22:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-13  9:35 [Caml-list] Timeouts and event Nicolas George
2001-09-13 19:16 ` Eric C. Cooper
2001-09-13 22:04   ` Nicolas George

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).