Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy is not threadsafe #4721

Closed
vicuna opened this issue Feb 19, 2009 · 4 comments
Closed

Lazy is not threadsafe #4721

vicuna opened this issue Feb 19, 2009 · 4 comments

Comments

@vicuna
Copy link

vicuna commented Feb 19, 2009

Original bug ID: 4721
Reporter: yminsky
Assigned to: @damiendoligez
Status: resolved (set by @alainfrisch on 2016-12-06T15:02:54Z)
Resolution: suspended
Priority: normal
Severity: feature
Version: 3.11.0
Category: standard library
Monitored by: mehdi @glondu

Bug description

As 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.

@vicuna
Copy link
Author

vicuna commented Feb 20, 2009

Comment author: vnicollet

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).

@vicuna
Copy link
Author

vicuna commented Mar 16, 2009

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented May 12, 2010

Comment author: @damiendoligez

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.

@vicuna
Copy link
Author

vicuna commented Dec 6, 2016

Comment author: @alainfrisch

Marking as suspended. No planned work on this front.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants