I have a function inside a module that I need to be thread-safe. The function looks like:
function foo()
for i=1:n
sigma = (calculate stuff)
end
return sigma
end
Of course, the above code doesn’t work, because sigma is local to the for loop. So I define it as a global:
function foo()
for i=1:n
global sigma = (calculate stuff)
end
return sigma
end
Question: is the resulting global sigma local to the calling thread, or have I introduced a race condition? If the latter, what’s the idiomatic way to define the variable as local to the thread? I don’t particularly want to introduce a lock on the variable, which would introduce some inefficiency in the code.