<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2009/10/e4edf6464acc506dba82bd0f07a9ce6e"
  from="Philippe Wang &lt;philippe.wang.lists@g...&gt;"
  author="Philippe Wang"
  date="2009-10-26T23:13:25"
  subject="Re: [Caml-list] threads, signals, and timeout"
  prev="2009/10/7518f0ec43a7d0512807efa6c0e8f252"
  next="2009/10/cb62aba28e1f84b31e18b738aad34fde"
  prev-in-thread="2009/10/ef5604af293b4607fcb3dec0a899701a"
  prev-thread="2009/10/de24601a1c9d4b35b8c7ad522418043d"
  next-thread="2009/10/cb62aba28e1f84b31e18b738aad34fde"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="threads, signals, and timeout">
<msg 
  url="2009/10/11a0067350cafabdd4315ce510c11474"
  from="yoann padioleau &lt;pad.aryx@g...&gt;"
  author="yoann padioleau"
  date="2009-10-26T18:08:43"
  subject="threads, signals, and timeout">
<msg 
  url="2009/10/87ae3673fe1c6ffe4b22356b236fdc55"
  from="Till Varoquaux &lt;till@p...&gt;"
  author="Till Varoquaux"
  date="2009-10-26T18:36:13"
  subject="Re: [Caml-list] threads, signals, and timeout">
<msg 
  url="2009/10/7be394e1425a4fd37e84d4697217f0f3"
  from="yoann padioleau &lt;pad.aryx@g...&gt;"
  author="yoann padioleau"
  date="2009-10-26T19:06:21"
  subject="Re: [Caml-list] threads, signals, and timeout">
<msg 
  url="2009/10/7518f0ec43a7d0512807efa6c0e8f252"
  from="Gerd Stolpmann &lt;gerd@g...&gt;"
  author="Gerd Stolpmann"
  date="2009-10-26T22:09:35"
  subject="Re: [Caml-list] threads, signals, and timeout">
</msg>
<msg 
  url="2009/10/ef5604af293b4607fcb3dec0a899701a"
  from="Gabriel Kerneis &lt;kerneis@p...&gt;"
  author="Gabriel Kerneis"
  date="2009-10-27T10:01:24"
  subject="Re: [Caml-list] threads, signals, and timeout">
</msg>
</msg>
</msg>
<msg 
  url="2009/10/e4edf6464acc506dba82bd0f07a9ce6e"
  from="Philippe Wang &lt;philippe.wang.lists@g...&gt;"
  author="Philippe Wang"
  date="2009-10-26T23:13:25"
  subject="Re: [Caml-list] threads, signals, and timeout">
</msg>
</msg>
</thread>

<contents>
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 &lt;pad.aryx@gmail.com&gt; wrote:
&gt; Hi,
&gt;
&gt; I would like to create different threads where each thread do some
&gt; computation and are subject to different
&gt; timeout. Without threads I usually use Unix.alarm with a SIGALARM
&gt; handler that just raise a Timeout exception
&gt; and everything works fine, but when I try to do something similar with
&gt; threads it does not work
&gt; because apparently the Unix.alarm done in one thread override the
&gt; Unix.alarm done in another
&gt; thread. I had a look at thread.mli but was not able to find anything
&gt; related to timeout.
&gt; Is there a way to have multiple timeout and multiple threads at the same time ?
&gt;
&gt; Here is a program that unforunately get the first timeout, but not the second :(
&gt;
&gt;
&gt; (*
&gt; ocamlc -g -thread unix.cma threads.cma signals_and_threads.ml
&gt; *)
&gt;
&gt; exception Timeout
&gt;
&gt; let mytid () =
&gt;  let t = Thread.self () in
&gt;  let i = Thread.id t in
&gt;  i
&gt;
&gt; let set_timeout () =
&gt;
&gt;  Sys.set_signal Sys.sigalrm
&gt;    (Sys.Signal_handle (fun _ -&gt;
&gt;      prerr_endline "Time is up!";
&gt;      print_string (Printf.sprintf "id: %d\n" (mytid()));
&gt;      raise Timeout
&gt;    ));
&gt;
&gt;  ignore(Unix.alarm 1);
&gt;  ()
&gt;
&gt;
&gt; let main =
&gt;  let t1 =
&gt;    Thread.create (fun () -&gt;
&gt;      set_timeout ();
&gt;      print_string (Printf.sprintf "t1 id: %d\n" (mytid()));
&gt;
&gt;      let xs = [1;2;3] in
&gt;      while(true) do
&gt;        let _ = List.map (fun x -&gt; x + 1) xs in
&gt;        ()
&gt;      done;
&gt;      ()
&gt;    ) ()
&gt;  in
&gt;
&gt;  let t2 =
&gt;    Thread.create (fun () -&gt;
&gt;      set_timeout ();
&gt;      print_string (Printf.sprintf "t2 id: %d\n" (mytid()));
&gt;
&gt;      let xs = [1;2;3] in
&gt;      while(true) do
&gt;        let _ = List.map (fun x -&gt; x + 1) xs in
&gt;        ()
&gt;      done;
&gt;      ()
&gt;    ) ()
&gt;  in
&gt;  Thread.join t1;
&gt;  Thread.join t2;
&gt;  ()
&gt;
&gt; ------------------
&gt;
&gt;
&gt;
&gt;
&gt; Here is the output
&gt; Time is up!
&gt; t2 id: 2
&gt; t1 id: 1
&gt; id: 1
&gt; Thread 1 killed on uncaught exception Signals_and_threads.Timeout
&gt; .... &lt;the program loops, meaning the second thread never received its timeout
&gt;

</contents>

</message>

