From: Alessandro Baretta <alex@baretta.com>
To: nadji@noos.fr
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] Native Threads
Date: Sun, 06 Oct 2002 16:34:07 +0200 [thread overview]
Message-ID: <3DA049DF.7030902@baretta.com> (raw)
In-Reply-To: <3DA01AAF.14FD242C@noos.fr>
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
next prev parent reply other threads:[~2002-10-06 14:24 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-06 11:12 nadji
2002-10-06 11:35 ` Sven LUTHER
2002-10-06 14:34 ` Alessandro Baretta [this message]
2002-10-06 15:02 ` nadji
2002-10-13 8:43 ` Xavier Leroy
2002-10-13 9:04 ` Sven LUTHER
2002-10-13 10:42 ` Stefano Zacchiroli
2002-10-13 11:26 ` Sven LUTHER
2002-10-13 10:45 ` nadji
2002-10-13 15:21 ` Alessandro Baretta
2002-10-13 15:26 ` nadji
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=3DA049DF.7030902@baretta.com \
--to=alex@baretta.com \
--cc=caml-list@inria.fr \
--cc=nadji@noos.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