Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0004721OCamlstandard librarypublic2009-02-19 13:032016-12-06 16:02
Assigned Todoligez 
PrioritynormalSeverityfeatureReproducibilityhave not tried
PlatformOSOS Version
Product Version3.11.0 
Target VersionFixed in Version 
Summary0004721: Lazy is not threadsafe
DescriptionAs reported on the caml-list by Victor Nicollet. This code:

 let expr = lazy (Thread.delay 1.0)

 let _ =
   let thread = Thread.create (fun () -> Lazy.force expr) () in
     Lazy.force expr;
     Thread.join thread

throws Lazy.Undefined. The documentation indicates that Lazy.Undefined is thrown when multiple recursive calls are made to Lazy.force, but clearly threads can trigger the same behavior.

It would be lovely if Lazy.force was threadsafe (i.e., if the second call simply blocked until the first call was complete). In the absence of that, I think the documentation should be updated to indicate that Lazy.t is not threadsafe.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
vnicollet (reporter)
2009-02-20 14:34

My suggestion would be to replace the internal raise_undefined in:

  Obj.set_field (Obj.repr blk) 0 raise_undefined;

with a function that remembers the thread it was created in and only raises Undefined within its owner thread : otherwise, it blocks until the value is computed by the original thread and then recursively performs the equivalent of Lazy.force on the value again.

This creates light overhead (since the closure that contains the thread identifier has to be created when evaluating the lazy block) but avoids having to wrap the entire force_lazy_block in a critical section (only the initial set_field has to be wrapped).
doligez (administrator)
2009-03-16 15:34

> I think the documentation should be updated to indicate that Lazy.t is not threadsafe.

I'd rather change the documentation to indicate that nothing is thread-safe except when explicitly noted.
doligez (administrator)
2010-05-12 16:54

I have documented the fact that Lazy.force is not thread-safe. Keeping this PR as a feature wish in case we find a good idea to make it thread-safe without too much overhead.
frisch (developer)
2016-12-06 16:02

Marking as suspended. No planned work on this front.

- Issue History
Date Modified Username Field Change
2009-02-19 13:03 yminsky New Issue
2009-02-20 14:34 vnicollet Note Added: 0004840
2009-03-16 15:34 doligez Note Added: 0004858
2009-04-29 15:41 doligez Status new => assigned
2009-04-29 15:41 doligez Assigned To => doligez
2010-05-12 16:54 doligez Note Added: 0005452
2010-05-12 16:54 doligez Severity minor => feature
2012-07-27 12:52 frisch Category OCaml general => OCaml standard library
2016-12-06 16:02 frisch Note Added: 0016643
2016-12-06 16:02 frisch Status assigned => resolved
2016-12-06 16:02 frisch Resolution open => suspended
2017-02-23 16:43 doligez Category OCaml standard library => standard library

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker