Threads.@threads
should not allow closures that contain a Core.Box
.
#57181
Labels
multithreading
Base.Threads and related functionality
(Same issue as JuliaFolds2/OhMyThreads.jl#133)
Any time a user references an outer-local-variable inside a
@threads
block, they are creating a silent race condition, because inside the@threads
block, accesses/re-assignments to that variable are simply reading/mutating aCore.Box
.Minimal demo:
gives
I'm pretty sure it's always a race condition to pass a closure with a
Core.Box
in it toThreads.@threads
, even if in many cases the race condition is highly improbable to hit, therefore I think it'd actually be beneficial if we turned theres1
loop into an unconditional error.This is especially important for
@threads
because it's not clear to the user that it creates a closure. A recent example in the wild of a user hitting this issue can be found here: https://discourse.julialang.org/t/why-does-julia-threads-in-the-following-way/124953The text was updated successfully, but these errors were encountered: