Another way, which is safe, is to use a lock:
julia> using ChunkSplitters
function foo(data; nchunks=Threads.nthreads())
lk = ReentrantLock()
iwin, datawin = nothing, nothing
Threads.@threads for (i_range, _) in chunks(data, nchunks)
for i in i_range
if !isnothing(iwin)
break
end
# complex computation here
lock(lk) do
if isnothing(iwin)
if data[i] == 10
iwin, datawin = i, data[i]
end
end
end
end
end
return iwin, datawin
end
foo (generic function with 1 method)
julia> a = foo(test)
(29, 10)