From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: **** X-Spam-Status: No, score=4.1 required=5.0 tests=AWL,DNS_FROM_RFC_POST, DNS_FROM_SECURITYSAGE,SPF_NEUTRAL autolearn=disabled version=3.1.3 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 B2E49BB84 for ; Sun, 9 Nov 2008 19:02:41 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvUAACu0FklIDtydimdsb2JhbACTYz4BAQEKCQwHDwWpGopBAQMBA4NVggg X-IronPort-AV: E=Sophos;i="4.33,569,1220220000"; d="scan'208";a="19770596" Received: from discorde.inria.fr ([192.93.2.38]) by mail1-smtp-roc.national.inria.fr with ESMTP; 09 Nov 2008 19:02:41 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id mA9I2feh004142 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Sun, 9 Nov 2008 19:02:41 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AvUAACu0FklIDtydimdsb2JhbACTYz4BAQEKCQwHDwWpGopBAQMBA4NVggg X-IronPort-AV: E=Sophos;i="4.33,569,1220220000"; d="scan'208";a="19770595" Received: from fg-out-1718.google.com ([72.14.220.157]) by mail1-smtp-roc.national.inria.fr with ESMTP; 09 Nov 2008 19:02:41 +0100 Received: by fg-out-1718.google.com with SMTP id e21so1895663fga.25 for ; Sun, 09 Nov 2008 10:02:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:from:to :in-reply-to:content-type:content-transfer-encoding:mime-version :subject:date:references:x-mailer:sender; bh=+Ngyuwk9FulweTDV05tVr+GfCvR0llFAl63+HQOS3mE=; b=Y5WxhwTHl8kVVy8hvED7Fpz2rQnmorA5ENOuAobu9rr6gWO5bxuyM5x51vdLk0TWj7 lSPejtJSOakFiMrnsDDcJbp8LSterCyGvaKE0/iDWi+RyIvc6TZRw9/ArTZ1PImAEkE9 22YfSdehBF9XFWrL5+LvyU9PaNJ8qpHcy4zUU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:from:to:in-reply-to:content-type :content-transfer-encoding:mime-version:subject:date:references :x-mailer:sender; b=i3StB2ZZfxaqW7CjrTBY4OhxJh5xrloMLkEJYbw1ohyvMYssVa1cDyInESdrOAk1Tc kgXwwn8j5XmvKYAZuaBFPt19i0FbpzcVBXX97VP9bJKr0gjErtwgFC1S6oU56Lj39FsT sqwoJ+tTo9FR+oi3WCnNOjVCOwxk5Ze44hbCQ= Received: by 10.181.33.18 with SMTP id l18mr1802454bkj.192.1226253760452; Sun, 09 Nov 2008 10:02:40 -0800 (PST) Received: from ?192.168.1.34? (156-50.77-83.cust.bluewin.ch [83.77.50.156]) by mx.google.com with ESMTPS id d6sm8952201fga.2.2008.11.09.10.02.39 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sun, 09 Nov 2008 10:02:40 -0800 (PST) Message-Id: <3C8EEBE9-DE88-4D57-AD41-11FD043A8DF9@erratique.ch> From: =?ISO-8859-1?Q?Daniel_B=FCnzli?= To: OCaml Mailing List In-Reply-To: <7CB71A72-D78E-4C00-B021-236113D3289F@erratique.ch> Content-Type: text/plain; charset=ISO-8859-1; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Apple Message framework v929.2) Subject: Sleeping and waking up a thread (was Re: Sending a signal to a thread) Date: Sun, 9 Nov 2008 18:59:47 +0100 References: <7CB71A72-D78E-4C00-B021-236113D3289F@erratique.ch> X-Mailer: Apple Mail (2.929.2) Sender: =?UTF-8?B?RGFuaWVsIELDvG56bGk=?= X-Miltered: at discorde with ID 491725C1.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; bunzli:01 buenzli:01 bunzli:01 woke:98 threads:01 unix:01 unix:01 variables:02 sys:03 sys:03 mutex:03 mutex:03 latter:03 workaround:03 handles:03 Le 9 nov. 08 =E0 16:00, Daniel B=FCnzli a =E9crit : > Is there a way to send a signal to a thread from another thread =20 > (i.e. something like pthread_kill) ? Sorry to respond to myself. This is not the answer to the question =20 (which I believe is no) but it does solve my problem. The actual problem was to be able to sleep a thread for a specific =20 amount of time unless another thread woke it before. For the latter =20 operation I just wanted to send a sigalrm to the thread from the other =20= thread since this signal is already used to manage the sleep timer. Anyway there's a workaround with condition variables. See the code =20 below (n.b. handles only one sleeping thread, as the signal handler is =20= shared between threads.) Best, Daniel let sleep, wakeup =3D let m =3D Mutex.create () in let proceed =3D Condition.create () in let sleeping =3D ref false in let set_timer d =3D let s =3D { Unix.it_interval =3D 0.; it_value =3D d } in ignore (Unix.setitimer Unix.ITIMER_REAL s) in let sleep d =3D (* with d =3D 0. = unbounded =20 sleep. *) Mutex.lock m; sleeping :=3D true; set_timer d; while !sleeping do Condition.wait proceed m done; Mutex.unlock m in let wakeup () =3D Mutex.lock m; sleeping :=3D false; set_timer 0.; Mutex.unlock m; Condition.signal proceed in let timer _ =3D sleeping :=3D false; Condition.signal proceed in Sys.set_signal Sys.sigalrm (Sys.Signal_handle timer); sleep, wakeup