Groups 182 of 99+ julia-users › Do threads in a threaded loop have their own scope? 4 posts by 2 authors Evan Fields Oct 11 Let's say I have a type MyType and function f(mt::MyType) which is slow and stochastic. I have an object y::MyType, and I'd like to compute f(y) many times. If I write a loop like fvals = Vector{Float64}(100) Threads.@threads for i in 1:length(fvals) ycopy = deepcopy(y) fvals[i] = f(ycopy) end The various loop iterations are not interfering with each other, right? Each has its own copy of y? Yichao Yu Oct 11 - show quoted text - Normal variables have the same scope behavior as a normal loops so they are bit shared if it is not used outside the loop. Evan Fields Oct 11 I'm unsure if "bit shared" is a technical term I should know, or if "bit shared" is a smartphone typo for "not shared" which would describe my understanding of normal loops, where it seems each iteration doesn't have access to loop-only variables defined in a previous iteration. :) I guess the better question is if I want to run my f(mt::MyType) simulations in parallel, what's the best way to do so? Yichao Yu Oct 11 On Tue, Oct 11, 2016 at 1:13 PM, Evan Fields wrote: I'm unsure if "bit shared" is a technical term I should know, or if "bit shared" is a smartphone typo for "not shared" which would describe my understanding of normal loops, where it seems each iteration doesn't have access to loop-only variables defined in a previous iteration. :) Right, it's "not" shared typed on my phone....... I don't know what exactly I typed....... I guess the better question is if I want to run my f(mt::MyType) simulations in parallel, what's the best way to do so? The code looks reasonably correct although it's hard to say what's the best way. E.g. if you really need a deep copy of `y`, or if `f` is very expensive and self-contained it might be better to just use a worker process (which can be run on multiple machines).