# Global variables "leak" during mulithreading

**URL:** <https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [May 11, 2022, 5:27pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900 "2022-05-11T17:27:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Shen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_shen1/32/33160_2.png) [@Kevin\_Shen1](https://discourse.julialang.org/u/Kevin_Shen1)\
**Post date:** [May 11, 2022, 5:27pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/1 "2022-05-11T17:27:57Z")

</div>

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:

```julia
@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.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 11, 2022, 5:30pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/2 "2022-05-11T17:30:01Z")

</div>

If it’s global - there’s exactly one copy that all threads can access.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 11, 2022, 5:30pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/3 "2022-05-11T17:30:38Z")

</div>

> [@Kevin\_Shen1](#):
>
> Logically this shouldn’t be happening since the two threads are separated by different calls to my\_function.

threads is shared-memory parallelism, so if the global variable is mutable and used in a non-thread-safe way, this is expected to happen.

---

<div class="post-metadata">

**Author:** ![Kevin\_Shen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_shen1/32/33160_2.png) [@Kevin\_Shen1](https://discourse.julialang.org/u/Kevin_Shen1)\
**Post date:** [May 11, 2022, 5:37pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/4 "2022-05-11T17:37:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 11, 2022, 5:42pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/5 "2022-05-11T17:42:46Z")

</div>

Do you need a per-thread accumulator? Or do you just need a thread-safe data structure that all threads can use?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 11, 2022, 5:42pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/6 "2022-05-11T17:42:50Z")

</div>

TLDR is don’t use global variables. put the loop in a function and have a variable in that function.

---

<div class="post-metadata">

**Author:** ![Kevin\_Shen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_shen1/32/33160_2.png) [@Kevin\_Shen1](https://discourse.julialang.org/u/Kevin_Shen1)\
**Post date:** [May 11, 2022, 5:48pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/7 "2022-05-11T17:48:40Z")

</div>

Each thread is running completely independently.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 11, 2022, 5:49pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/8 "2022-05-11T17:49:48Z")

</div>

`@threads` is arbitrarily splitting the work to different threads, so I’m trying to determine what the point is of a thread-specific global?

---

<div class="post-metadata">

**Author:** ![Kevin\_Shen1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin_shen1/32/33160_2.png) [@Kevin\_Shen1](https://discourse.julialang.org/u/Kevin_Shen1)\
**Post date:** [May 11, 2022, 5:51pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/9 "2022-05-11T17:51:18Z")

</div>

I think I misused global, I was just trying to update a variable in a loop (as I’m sure many new users to Julia would try to do).

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 11, 2022, 5:53pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/10 "2022-05-11T17:53:53Z")

</div>

I understand, but let’s say you’re doing this:

```julia
i = 1
for x in 1:100
    i += 1
end

```

And you want to parallelize this. Why do you want a copy of `i` for each thread? Are you bringing them back together at the end somehow?

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [May 11, 2022, 6:06pm UTC](https://discourse.julialang.org/t/global-variables-leak-during-mulithreading/80900/11 "2022-05-11T18:06:39Z")

</div>

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).
