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=0.6 required=5.0 tests=NO_REAL_NAME autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id CF5C9BB84 for ; Tue, 17 Feb 2009 16:55:51 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmoDAFdtmklbecdp/2dsb2JhbACBbpFZwAGEEwY X-IronPort-AV: E=Sophos;i="4.38,223,1233529200"; d="scan'208";a="21240479" Received: from rps1805.ovh.net (HELO nicollet.net) ([91.121.199.105]) by mail2-smtp-roc.national.inria.fr with SMTP; 17 Feb 2009 16:55:41 +0100 Received: (qmail 2954 invoked by uid 1000); 17 Feb 2009 16:54:55 +0100 Date: Tue, 17 Feb 2009 16:54:55 +0100 From: victor-caml-list@nicollet.net To: caml-list@inria.fr Subject: Lazy and Threads Message-ID: <20090217155455.GA2352@stock.ovh.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.13 (2006-08-11) X-Spam: no; 0.00; expr:01 expr:01 1.0:98 threads:01 threads:01 undefined:01 undefined:01 closure:01 closure:01 lazy:02 lazy:02 functional:02 functional:02 mutex:03 let:03 Hello, I'm working with both lazy expressions and threads, and noticed that the evaluation of lazy expressions is not thread-safe: let expr = lazy (Thread.delay 1.0) let _ = let thread = Thread.create (fun () -> Lazy.force expr) () in Lazy.force expr; Thread.join thread The second thread to attempt an evaluation dies with Lazy.Undefined. The source of this appears to be the standard Lazy.force behavior, which replaces the closure in the lazy-block with a raise_undefined closure in order to detect self-recursing lazy expressions. Aside from handling a mutex myself (which I don't find very elegant for a read operation in a pure functional program) is there a solution I can use to manipulate lazy expressions in a pure functional multi-threaded program? -- Victor Nicollet