Description
This is one way to at least warn users that code like the code shown in #132 is incorrect. The idea would be that when a user calls
tmap(f, itr)
(or tmapreduce
or whatever), we want to check if any of the fields of f
are a Core.Box
, which would signal that the user accidentally has caused a concurrency violation.
Here's a fun example:
julia> let
A = 1
res1 = tmap(1:10) do i
A = i
sleep(rand())
A
end
res2 = tmap(1:10) do i
local A = i
sleep(rand())
A
end
@info "Oops!" res1' res2'
end
┌ Info: Oops!
│ res1' =
│ 1×10 adjoint(::Vector{Int64}) with eltype Int64:
│ 8 2 10 8 4 2 6 8 10 4
│ res2' =
│ 1×10 adjoint(::Vector{Int64}) with eltype Int64:
└ 1 2 3 4 5 6 7 8 9 10
The problem is that the A=1
inside the let block is making it so that inside the tmap
for res1
, A
actually is a Core.Box
and every time we assign to A
, we are mutating the Core.Box
as a race condition, and each time we reference A
, we're making a racey read.
The Core.Box
design for closures is fine for sequential programs, but is wildly incorrect for concurrenct programs. This should actually be pretty easy to detect and throw an informative error.
I don't think there is any valid use-case where you'd put a boxed closure into one of our threaded functions and not cause a data-race.