# Multithreading unstable with JuMP

**URL:** <https://discourse.julialang.org/t/multithreading-unstable-with-jump/49494>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, multithreading\
**Created:** [November 3, 2020, 1:05am UTC](https://discourse.julialang.org/t/multithreading-unstable-with-jump/49494 "2020-11-03T01:05:53Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [November 9, 2020, 12:49am UTC](https://discourse.julialang.org/t/multithreading-unstable-with-jump/49494/14 "2020-11-09T00:49:54Z")

</div>

> [@JohnZ](#):
>
> I do not understand how a race condition can arise for assignment outside the `@threads` loop.

I hit this same issue recently, see the [link](https://discourse.julialang.org/t/race-condition-with-local-variable-in-threads-for-loop/49163). julia scope rule is not very intuitively in this case. An assignment to a variable, before or after, the `@threads` for loop will make it have an scope outside the threads section.

Run this example `julia t3 test.jl`

```julia
# test.jl
import Base.Threads: @threads, nthreads

function test()
    @threads :static for i in 1:100

        d = Dict(:a => Dict())
        a = d[:a]
        @assert a === d[:a]

        # Line 12: This throw an AssertionError
        for j in 1:100; @assert a === d[:a] end 
    end
    a = [] # If this line is commented the problem disappears 
end

println("Running test (-t$(nthreads()))")
for i = 1:500; test() end

```

---

_[View the full topic](https://discourse.julialang.org/t/multithreading-unstable-with-jump/49494)._
