From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id DFFF3BC37 for ; Mon, 26 Oct 2009 19:36:12 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ai4DACuJ5UrRVd/BmGdsb2JhbACEcop7gguJAT8BAQEBAQYLDAcTrRuBY4VGiGgBAwMFgS2COlMEiUQ X-IronPort-AV: E=Sophos;i="4.44,627,1249250400"; d="scan'208";a="37033777" Received: from mail-iw0-f193.google.com ([209.85.223.193]) by mail3-smtp-sop.national.inria.fr with ESMTP; 26 Oct 2009 19:36:11 +0100 Received: by iwn31 with SMTP id 31so6482811iwn.0 for ; Mon, 26 Oct 2009 11:36:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=7SdPLMrEGXGAaLX/u+yFrjpfrr3Yfdzp9hiM0fKjW5Q=; b=VcQkKps68lkV0u5Hs73xIXGj6/eWOo6MZ+4sprzMplS2q6XoFExYwW1O/xzjRENVSZ LFT763w5yfT8xQXfv34FMKMk6kLpUulRi+9BFOl0WpoyPsa/VQvwyXFGHgoboyHTmk9U g6mddP8hHVxawDawpURzdUAXxovqAJ0yG7Cac= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=RfmtpY64FMaBWGl2XlVCOF4F8rxsyx4lQGZdqZo1tulnBsG63k3izdOSKkfpndlbHf 3P5H5p3KMek4b5yP4dqp6lnhd8jH7Un/jDxMoWkVcTfVB/fRiDsdkE41lnT2/+qziidW engG7pjf8Q6Nq/Xsgx74ywGEW36dWc0P6A75g= MIME-Version: 1.0 Sender: till.varoquaux@gmail.com Received: by 10.231.124.166 with SMTP id u38mr865814ibr.17.1256582171273; Mon, 26 Oct 2009 11:36:11 -0700 (PDT) In-Reply-To: References: Date: Mon, 26 Oct 2009 14:36:10 -0400 X-Google-Sender-Auth: ffca831797c198d6 Message-ID: <9d3ec8300910261136w3cee8329od4b0a02365cde8db@mail.gmail.com> Subject: Re: [Caml-list] threads, signals, and timeout From: Till Varoquaux To: yoann padioleau Cc: caml-list@inria.fr Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; model:01 invocation:01 computations:01 computations:01 ocaml:01 ocaml:01 opengroup:01 onlinepubs:01 mli:01 ocamlc:01 -thread:01 prerr:01 endline:01 printf:01 sprintf:01 You'd have the same problem in any other programming language; this is due to the underlying POSIX model. The POSIX standard mentions that alarms are global to the process [1]. " There were two possible choices for alarm generation in multi-threaded applications: generation for the calling thread or generation for the process. The first option would not have been particularly useful since the alarm state is maintained on a per-process basis and the alarm that is established by the last invocation of alarm() is the only one that would be active." As a general matter mixing signals and threads is a thorny issue under unix; you should avoid doing so unless you have a good grasp of what is going to happen. Having a global timer thread that kills timed-out threads would be possible but seems very painful. If you give us more information on what you are trying to achieve (eg your computations threads are crunshing numbers in a tight loop or you are performing io etc...) we might be able to guide you. Here is a quick run down of my flawed and partial understand of the world: For heavy computations threads will not help you. I am sure you are aware of this but there never is more than one ocaml thread running at the same time. If you can fork and collect the resulting data through pipes or shared memory you should be able to get a noticeable performance boost. There have been countless posts on the subject; I am too lazy to google them up. I/O is a very different beast and you are probably better of with an asynchronous framework. Luckily for you Ocaml has a pretty good one already available [2] Till [1]: http://www.opengroup.org/onlinepubs/000095399/functions/alarm.html [2]: http://ocsigen.org/lwt/ On Mon, Oct 26, 2009 at 2:08 PM, yoann padioleau 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 se= cond :( > > > (* > ocamlc -g -thread unix.cma threads.cma signals_and_threads.ml > *) > > exception Timeout > > let mytid () =3D > =C2=A0let t =3D Thread.self () in > =C2=A0let i =3D Thread.id t in > =C2=A0i > > let set_timeout () =3D > > =C2=A0Sys.set_signal Sys.sigalrm > =C2=A0 =C2=A0(Sys.Signal_handle (fun _ -> > =C2=A0 =C2=A0 =C2=A0prerr_endline "Time is up!"; > =C2=A0 =C2=A0 =C2=A0print_string (Printf.sprintf "id: %d\n" (mytid())); > =C2=A0 =C2=A0 =C2=A0raise Timeout > =C2=A0 =C2=A0)); > > =C2=A0ignore(Unix.alarm 1); > =C2=A0() > > > let main =3D > =C2=A0let t1 =3D > =C2=A0 =C2=A0Thread.create (fun () -> > =C2=A0 =C2=A0 =C2=A0set_timeout (); > =C2=A0 =C2=A0 =C2=A0print_string (Printf.sprintf "t1 id: %d\n" (mytid()))= ; > > =C2=A0 =C2=A0 =C2=A0let xs =3D [1;2;3] in > =C2=A0 =C2=A0 =C2=A0while(true) do > =C2=A0 =C2=A0 =C2=A0 =C2=A0let _ =3D List.map (fun x -> x + 1) xs in > =C2=A0 =C2=A0 =C2=A0 =C2=A0() > =C2=A0 =C2=A0 =C2=A0done; > =C2=A0 =C2=A0 =C2=A0() > =C2=A0 =C2=A0) () > =C2=A0in > > =C2=A0let t2 =3D > =C2=A0 =C2=A0Thread.create (fun () -> > =C2=A0 =C2=A0 =C2=A0set_timeout (); > =C2=A0 =C2=A0 =C2=A0print_string (Printf.sprintf "t2 id: %d\n" (mytid()))= ; > > =C2=A0 =C2=A0 =C2=A0let xs =3D [1;2;3] in > =C2=A0 =C2=A0 =C2=A0while(true) do > =C2=A0 =C2=A0 =C2=A0 =C2=A0let _ =3D List.map (fun x -> x + 1) xs in > =C2=A0 =C2=A0 =C2=A0 =C2=A0() > =C2=A0 =C2=A0 =C2=A0done; > =C2=A0 =C2=A0 =C2=A0() > =C2=A0 =C2=A0) () > =C2=A0in > =C2=A0Thread.join t1; > =C2=A0Thread.join t2; > =C2=A0() > > ------------------ > > > > > 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 > .... > _______________________________________________ > 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 >