Using global variables inside a function (executed separately) with multithreading seems to cause multiple threads to incorrect share/access the global variables.
It’s a bit contrived for me to present a concrete examples but my code looks something like this:
@threads for (i, params) in collect(enumerate(my_list))
my_function(i, params)
end
Inside my_function I use a global variable. The program is crashing with n>=2 threads because somehow one thread is making reference to the global variable value of another thread. The error message I got was basically: (in thread A) dictionary doesn’t have access to key X where X would be a value in thread B. Logically this shouldn’t be happening since the two threads are separated by different calls to my_function.
The reason I was using a global variable inside my_function was I had a while-loop where I was updating the variable every iteration. After modifying my implementation to use a growing array instead of a global variable, my program no longer crashed.
This would be strange if it’s expected behavior. If it isn’t, it seems like a serious bug with multithreading and global variables.
I see. Sorry my mistake. Is there a good way to update variables in for-loops and while-loops other than using global variables then?
There seems to be a beginner trap here. Trying to update variables in loops like in Python → Google solution → find solution with global variable → use global variable → switch to multithreading and scratch your head (like I did) for 5h trying to debug.
Correct me if I’m wrong, but it seems like your mental model of what’s happening is that you’re running 8 separate copies of for loops (if you have 8 threads available), instead of one for loop where sets of 8 loop bodies are arbitrarily run in parallel?
So I think you really want a variable in the next-higher scope that can be safely updated by each thread. And that could be passed to my_function as an argument (probably renamed my_function! to indicate that it’ll modify its argument).