Browse thread
threads, signals, and timeout
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Philippe Wang <philippe.wang.lists@g...> |
| Subject: | Re: [Caml-list] threads, signals, and timeout |
Considering that posix signals are not real-time *anyway*, using them to programme specific treatments per-thread is hmmm... say a nightmare ! Plus I don't quite see how you could eventually have a non-broken implementation. Gerd Stolpmann emphasized it if I understood well. One solution would be to use state variables to check every once in a while. Or maybe to use "fairthreads" instead, but I guess that the problem is actually much more complicated than just that. Well, I thought I had more interesting things to say. I was wrong, then just my two cents. Anyways, good luck! Cheers, Philippe Wang On Mon, Oct 26, 2009 at 7:08 PM, yoann padioleau <pad.aryx@gmail.com> wrote: > Hi, > > I would like to create different threads where each thread do some > computation and are subject to different > timeout. Without threads I usually use Unix.alarm with a SIGALARM > handler that just raise a Timeout exception > and everything works fine, but when I try to do something similar with > threads it does not work > because apparently the Unix.alarm done in one thread override the > Unix.alarm done in another > thread. I had a look at thread.mli but was not able to find anything > related to timeout. > Is there a way to have multiple timeout and multiple threads at the same time ? > > Here is a program that unforunately get the first timeout, but not the second :( > > > (* > ocamlc -g -thread unix.cma threads.cma signals_and_threads.ml > *) > > exception Timeout > > let mytid () = > let t = Thread.self () in > let i = Thread.id t in > i > > let set_timeout () = > > Sys.set_signal Sys.sigalrm > (Sys.Signal_handle (fun _ -> > prerr_endline "Time is up!"; > print_string (Printf.sprintf "id: %d\n" (mytid())); > raise Timeout > )); > > ignore(Unix.alarm 1); > () > > > let main = > let t1 = > Thread.create (fun () -> > set_timeout (); > print_string (Printf.sprintf "t1 id: %d\n" (mytid())); > > let xs = [1;2;3] in > while(true) do > let _ = List.map (fun x -> x + 1) xs in > () > done; > () > ) () > in > > let t2 = > Thread.create (fun () -> > set_timeout (); > print_string (Printf.sprintf "t2 id: %d\n" (mytid())); > > let xs = [1;2;3] in > while(true) do > let _ = List.map (fun x -> x + 1) xs in > () > done; > () > ) () > in > Thread.join t1; > Thread.join t2; > () > > ------------------ > > > > > Here is the output > Time is up! > t2 id: 2 > t1 id: 1 > id: 1 > Thread 1 killed on uncaught exception Signals_and_threads.Timeout > .... <the program loops, meaning the second thread never received its timeout >