caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Eric C. Cooper" <ecc@cmu.edu>
To: Nicolas George <nicolas.george@ens.fr>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Timeouts and event
Date: Thu, 13 Sep 2001 15:16:11 -0400	[thread overview]
Message-ID: <3BA105FB.7500FD0@cmu.edu> (raw)
In-Reply-To: <20010913113508.A682@aimlin>

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


  reply	other threads:[~2001-09-13 19:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-13  9:35 Nicolas George
2001-09-13 19:16 ` Eric C. Cooper [this message]
2001-09-13 22:04   ` Nicolas George

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3BA105FB.7500FD0@cmu.edu \
    --to=ecc@cmu.edu \
    --cc=caml-list@inria.fr \
    --cc=nicolas.george@ens.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).