Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Lazy and Threads
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2009-02-17 (15:55)
From: victor-caml-list@n...
Subject: Lazy and Threads

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 

Victor Nicollet