* Sending a signal to a thread
@ 2008-11-09 15:00 Daniel Bünzli
2008-11-09 17:59 ` Sleeping and waking up a thread (was Re: Sending a signal to a thread) Daniel Bünzli
0 siblings, 1 reply; 2+ messages in thread
From: Daniel Bünzli @ 2008-11-09 15:00 UTC (permalink / raw)
To: OCaml Mailing List
Hello,
Is there a way to send a signal to a thread from another thread (i.e.
something like pthread_kill) ?
Best,
Daniel
^ permalink raw reply [flat|nested] 2+ messages in thread
* Sleeping and waking up a thread (was Re: Sending a signal to a thread)
2008-11-09 15:00 Sending a signal to a thread Daniel Bünzli
@ 2008-11-09 17:59 ` Daniel Bünzli
0 siblings, 0 replies; 2+ messages in thread
From: Daniel Bünzli @ 2008-11-09 17:59 UTC (permalink / raw)
To: OCaml Mailing List
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-11-09 18:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-09 15:00 Sending a signal to a thread Daniel Bünzli
2008-11-09 17:59 ` Sleeping and waking up a thread (was Re: Sending a signal to a thread) Daniel Bünzli
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox