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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id C35DABC37 for ; Tue, 27 Oct 2009 00:13:23 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AtUAAG3J5UrRVdrUkGdsb2JhbACRX4kcPwEBAQEJCQwHEwOtDoE7hXmIaAEDAwWEOgQ X-IronPort-AV: E=Sophos;i="4.44,628,1249250400"; d="scan'208";a="39000758" Received: from mail-bw0-f212.google.com ([209.85.218.212]) by mail1-smtp-roc.national.inria.fr with ESMTP; 27 Oct 2009 00:13:23 +0100 Received: by bwz4 with SMTP id 4so2737257bwz.22 for ; Mon, 26 Oct 2009 16:13:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=97LVLT+McOgIwB37rZ0ujN50gZt9OiyjBBVaD7ziA24=; b=e/J1lNh+m52pPyzV7lSM+hu19yALzHUQ+Q9o0Bk2qPf/Pu6cX6MPOkxntxmnEgxVCA XazNX72USaHxBZTwH5vrbG6x2cCbgNfzNqbxXlZXQuWMQHZFjKAK8WlxJMJkopY2ZqwX IyVkndjTZDTNS73JVku+ja9CUWAYM2o3puUuw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=QPSVMuMmRh69osxzsDp0bSwE9oU+9U+v5escJy6pg1t2Jx1DRf0yUKVggNFW3ClEe2 AgWqW5L11nk6ynaLksHyysW1IBlw2isZlICf/vvfuMMgPP0Y+AGd/G+e7QE9Um2hoy3h zmVvbXHwq4ecs0NH+zz85ON8GaPpYfX4nA53s= MIME-Version: 1.0 Received: by 10.239.163.204 with SMTP id q12mr620077hbd.39.1256598802765; Mon, 26 Oct 2009 16:13:22 -0700 (PDT) In-Reply-To: References: Date: Tue, 27 Oct 2009 00:13:22 +0100 Message-ID: <4d1b2df20910261613l7bd4a7a9yfde623d80c4cf9c5@mail.gmail.com> Subject: Re: [Caml-list] threads, signals, and timeout From: Philippe Wang To: yoann padioleau Cc: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; hmmm:01 gerd:01 stolpmann:01 fairthreads:01 cheers:01 mli:01 ocamlc:01 -thread:01 prerr:01 endline:01 printf:01 sprintf:01 printf:01 sprintf:01 uncaught:01 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 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 > ....