# Inconsistent results when using Threads.@threads in a loop

**URL:** <https://discourse.julialang.org/t/inconsistent-results-when-using-threads-threads-in-a-loop/100134>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [June 10, 2023, 9:18am UTC](https://discourse.julialang.org/t/inconsistent-results-when-using-threads-threads-in-a-loop/100134 "2023-06-10T09:18:33Z")\
**Posts on this page:** 1\
**Showing post:** 19

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 10, 2023, 8:21pm UTC](https://discourse.julialang.org/t/inconsistent-results-when-using-threads-threads-in-a-loop/100134/19 "2023-06-10T20:21:05Z")

</div>

It feels like the same realm as not being able to conditionally define methods for functions in local scopes, every method just exists at once. Issue 15602 states that dynamically creating functions is too slow and this allows functions to be hoisted to top-level (like a functor). That rationale wouldn’t apply to local variable existence, and I’m guessing lowering could be changed to differentiate the latter `v_re` and `v_im` without performance penalty, but both seem rooted in how the local scope is built during compilation. Another unintuitive implementation is how [global variables act like references rather than values in function inputs](https://discourse.julialang.org/t/should-a-reassigning-begin-block-execute-before-a-variables-value-is-set/100038).

> **Example of conditional methods not working in a local scope**
>
> ```julia
> julia> foofalse = let
> foo() = 0
> if false foo(x) = 1 end
> foo
> end
> (::var"#foo#1") (generic function with 2 methods)
> 
> julia> foo() = 0
> foo (generic function with 1 method)
> 
> julia> if false foo(x) = 1 end
> 
> julia> foo
> foo (generic function with 1 method)
> 
> julia> if true foo(x) = 1 end
> foo (generic function with 2 methods)
> 
> ```

---

_[View the full topic](https://discourse.julialang.org/t/inconsistent-results-when-using-threads-threads-in-a-loop/100134)._
