From: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
To: OCaml Mailing List <caml-list@inria.fr>
Subject: Sleeping and waking up a thread (was Re: Sending a signal to a thread)
Date: Sun, 9 Nov 2008 18:59:47 +0100 [thread overview]
Message-ID: <3C8EEBE9-DE88-4D57-AD41-11FD043A8DF9@erratique.ch> (raw)
In-Reply-To: <7CB71A72-D78E-4C00-B021-236113D3289F@erratique.ch>
Le 9 nov. 08 à 16:00, Daniel Bünzli a écrit :
> Is there a way to send a signal to a thread from another thread
> (i.e. something like pthread_kill) ?
Sorry to respond to myself. This is not the answer to the question
(which I believe is no) but it does solve my problem.
The actual problem was to be able to sleep a thread for a specific
amount of time unless another thread woke it before. For the latter
operation I just wanted to send a sigalrm to the thread from the other
thread since this signal is already used to manage the sleep timer.
Anyway there's a workaround with condition variables. See the code
below (n.b. handles only one sleeping thread, as the signal handler is
shared between threads.)
Best,
Daniel
let sleep, wakeup =
let m = Mutex.create () in
let proceed = Condition.create () in
let sleeping = ref false in
let set_timer d =
let s = { Unix.it_interval = 0.; it_value = d } in
ignore (Unix.setitimer Unix.ITIMER_REAL s)
in
let sleep d = (* with d = 0. unbounded
sleep. *)
Mutex.lock m;
sleeping := true;
set_timer d;
while !sleeping do Condition.wait proceed m done;
Mutex.unlock m
in
let wakeup () =
Mutex.lock m;
sleeping := false;
set_timer 0.;
Mutex.unlock m;
Condition.signal proceed
in
let timer _ = sleeping := false; Condition.signal proceed in
Sys.set_signal Sys.sigalrm (Sys.Signal_handle timer);
sleep, wakeup
prev parent reply other threads:[~2008-11-09 18:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-11-09 15:00 Sending a signal to a thread Daniel Bünzli
2008-11-09 17:59 ` Daniel Bünzli [this message]
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=3C8EEBE9-DE88-4D57-AD41-11FD043A8DF9@erratique.ch \
--to=daniel.buenzli@erratique.ch \
--cc=caml-list@inria.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