Is there a way to send a signal to a thread from another thread (i.e.
something like pthread_kill) ?
Best,
Daniel
_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs
> 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