From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 87F5CBB91 for ; Mon, 24 Jan 2005 15:44:28 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j0OEiROA017285 for ; Mon, 24 Jan 2005 15:44:27 +0100 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 PAA07229 for ; Mon, 24 Jan 2005 15:44:27 +0100 (MET) Received: from rproxy.gmail.com (rproxy.gmail.com [64.233.170.202]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j0OEiQQg017282 for ; Mon, 24 Jan 2005 15:44:26 +0100 Received: by rproxy.gmail.com with SMTP id 1so121918rny for ; Mon, 24 Jan 2005 06:44:26 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:date:from:to:cc:subject:message-id:mail-followup-to:references:mime-version:content-type:content-disposition:in-reply-to:user-agent; b=k2ZcItPD3nJW951p7EqVhyCU14uQp0WIEKDWLy0y8i8VXaEo4Azk0kTy+m93J6ulvtmdJiP+sDvXz8/PnrgFjcY00uXz1j9c3u14xeyA1ZpJauLsqEsoeui7MEWPEIa2Fr/GQDCzvSHXU6GUhrbLcqG4usKU2wxgo6jAGDuYOWU= Received: by 10.38.179.26 with SMTP id b26mr174380rnf; Mon, 24 Jan 2005 06:44:25 -0800 (PST) Received: from localhost ([141.155.88.179]) by smtp.gmail.com with ESMTP id 72sm1445rna.2005.01.24.06.44.25; Mon, 24 Jan 2005 06:44:25 -0800 (PST) Date: Mon, 24 Jan 2005 09:44:21 -0500 From: Markus Mottl To: yminsky@cs.cornell.edu Cc: SooHyoung Oh , Caml Mailing List Subject: Re: [Caml-list] Timeouts in Event module Message-ID: <20050124144421.GA9726@quant3.janestcapital.quant> Mail-Followup-To: yminsky@cs.cornell.edu, SooHyoung Oh , Caml Mailing List References: <891bd33905012305496c96b6d6@mail.gmail.com> <41F44701.4040500@compiler.kaist.ac.kr> <891bd339050123175618dc6acc@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <891bd339050123175618dc6acc@mail.gmail.com> User-Agent: Mutt/1.4.1i X-Miltered: at nez-perce with ID 41F509CB.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 41F509CA.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; markus:01 mottl:01 markus:01 mottl:01 caml-list:01 yaron:01 minsky:01 wrote:01 simulate:01 ocaml:01 watchdog:98 watchdog:98 ...:98 ...:98 thread:02 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.5 required=5.0 tests=INFO_TLD,RCVD_BY_IP autolearn=disabled version=3.0.0 X-Spam-Level: On Sun, 23 Jan 2005, Yaron Minsky wrote: > Thanks. That looks quite useful. I suppose one could create a > timed event by creating a channel and scheduling a callback to send on > that channel. It seems like a bit of a hack (not your library, but > the need to use such a mechanism to simulate timeouts), but it should > do the job. I remember having found the following solution, which I use in my projects now, on the list a while ago: let watchdog s v = let ch = Event.new_channel () in let watchdog_thread () = Thread.delay s; Event.sync (Event.send ch v) in ignore (Thread.create watchdog_thread ()); Event.receive ch let timed_sync s v event = Event.sync (Event.choose [event; watchdog s v]) You can use it e.g. as follows: let thread_fun ch = (* ... do something long ... *) Event.sync (Event.send result) in let ch = Event.new_channel () in let tid = Thread.create thread_fun ch in timed_sync 3.14 default_value (Event.receive ch) If "thread_fun" doesn't send + synchronize a result within 3.14 seconds on event channel "ch", then "default_value" will be returned, the result otherwise. Regards, Markus -- Markus Mottl markus.mottl@gmail.com http://www.ocaml.info