<?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/11a0067350cafabdd4315ce510c11474"
  from="yoann padioleau &lt;pad.aryx@g...&gt;"
  author="yoann padioleau"
  date="2009-10-26T18:08:43"
  subject="threads, signals, and timeout"
  prev="2009/10/de24601a1c9d4b35b8c7ad522418043d"
  next="2009/10/87ae3673fe1c6ffe4b22356b236fdc55"
  next-in-thread="2009/10/87ae3673fe1c6ffe4b22356b236fdc55"
  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>
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 _ -&gt;
      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 () -&gt;
      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 -&gt; x + 1) xs in
        ()
      done;
      ()
    ) ()
  in

  let t2 =
    Thread.create (fun () -&gt;
      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 -&gt; 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
.... &lt;the program loops, meaning the second thread never received its timeout

</contents>

</message>

