# 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:** 12

<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 8, 2020, 7:54pm UTC](https://discourse.julialang.org/t/multithreading-unstable-with-jump/49494/12 "2020-11-08T19:54:38Z")

</div>

You are maybe having a [race condition](https://discourse.julialang.org/t/race-condition-with-local-variable-in-threads-for-loop/49163) do to the `model` variable (and others) assignment outside the `@threads` loop. That can cause a thread to use a model that is not `optimize!` yet as pointed out by @odow.

> [@odow](#):
>
> `OptimizeNotCalled` suggests you are attempting querying the solution of a variable before calling `optimize!` .

That could explain also this:

> [@JohnZ](#):
>
> If I get rid of `Threads.@threads` before the for loop in `run_modelthreading(;α=0.05)` function then the error goes away.

It can be fixed (I think) in your `MWE` making the model asigment inside the `@threads` loop `local`

```julia
function run_modelthreading(;α=0.05)
    [...]
    # This make all this variables visibles between all threads
    model, x, y, γ = build_model(FutureValue,Loss,Demand,nproducts,prob,nscen,α)
    [...]
    Threads.@threads for i in 1:n
       [...]
       # This will force this variables to be local for each thread
       local model, x, y, γ = build_model(FutureValue,Loss,Demand,nproducts,prob,nscen,α)
       [...]
    end
    return [...]
end

```

---

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