From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id VAA22770; Thu, 13 Sep 2001 21:17:39 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id VAA22321 for ; Thu, 13 Sep 2001 21:17:38 +0200 (MET DST) Received: from ux9.sp.cs.cmu.edu (UX9.SP.CS.CMU.EDU [128.2.220.166]) by nez-perce.inria.fr (8.11.1/8.10.0) with SMTP id f8DJHbP05817 for ; Thu, 13 Sep 2001 21:17:37 +0200 (MET DST) Received: from c198429-a.ross1.pa.home.com by ux9.sp.cs.cmu.edu id aa26142; 13 Sep 2001 15:16 EDT Message-ID: <3BA105FB.7500FD0@cmu.edu> Date: Thu, 13 Sep 2001 15:16:11 -0400 From: "Eric C. Cooper" X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.9 i686) X-Accept-Language: en MIME-Version: 1.0 To: Nicolas George CC: caml-list@inria.fr Subject: Re: [Caml-list] Timeouts and event References: <20010913113508.A682@aimlin> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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