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 QAA16510; Sun, 6 Oct 2002 16:24:03 +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 QAA16386 for ; Sun, 6 Oct 2002 16:24:02 +0200 (MET DST) Received: from athlon.baretta.com (host186-68.pool80116.interbusiness.it [80.116.68.186]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g96EO1D04121 for ; Sun, 6 Oct 2002 16:24:01 +0200 (MET DST) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id 0AC4C273AE; Sun, 6 Oct 2002 16:34:08 +0200 (CEST) Message-ID: <3DA049DF.7030902@baretta.com> Date: Sun, 06 Oct 2002 16:34:07 +0200 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: nadji@noos.fr Cc: "caml-list@inria.fr" Subject: Re: [Caml-list] Native Threads References: <3DA01AAF.14FD242C@noos.fr> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk nadji@noos.fr wrote: > Hi, > Or, does someone knows how to implement a function > timeout: float -> (unit->unit) -> unit > which executes the function given in argument , but > no more than a certain amount of time, without Thread.kill ? > > TIA, > Nadji If you want synchronous killing you're in a mess, but if I can live with an asynchronous model, here's how you might go about with it. module type Exec_with_timeout = sig val run : bool ref val f : 'a -> unit val timeout : float end module Finite_time_thread (M:Exec_with_timeout) = struct let start arg = let kill_request () = Thread.delay M.timeout; M.run := false in let t1 = Thread.create M.f arg in let t2 = Thread.create kill_request () in Thread.join t1 end let module Foo : Exec_with_timeout = struct let run = ref true let f () = while !run do () (*Your code here*) done let timeout = 5.0 (* number of seconds this will run *) end in let Finite_foo_thread = Finite_time_thread Foo in Finite_foo_thread.start () This requires that Foo.f work cooperatively by periodically checking Foo.run to verify that it still has time. When, upon testing Foo.run, Foo.f discovers that it has consumed all the allotted time, it must return. Alex ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners