You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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).
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.
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.
The text was updated successfully, but these errors were encountered: