Volume of variables inside threaded for hinges?
In the following example
shared_arr = zeros(4000)
Threads.@threads for thread = 1:4
tmp_arr = rand(1000)
for i = 1:1000
shared_arr[(thread - 1)*1000+i] = tmp_arr[i]
end
end
I believe it shared_arr
is shared between all threads. Split tmp_arr
4 times so each thread has its own tmp_arr
?
+3
source to share
1 answer
According to the scoping rules described in the documentation, a new scope is introduced whenever the for-loop is called. Since it is tmp_arr
not declared before the loop, this will be a different value every iteration of the for loop. Note that it rand
may not be thread safe, however, per @Lyndon White's comment.
0
source to share