# Wrong results with multi-threading in Julia master

**URL:** <https://discourse.julialang.org/t/wrong-results-with-multi-threading-in-julia-master/1298>\
**Category:** General Usage\
**Created:** [January 5, 2017, 9:25am UTC](https://discourse.julialang.org/t/wrong-results-with-multi-threading-in-julia-master/1298 "2017-01-05T09:25:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 5, 2017, 9:25am UTC](https://discourse.julialang.org/t/wrong-results-with-multi-threading-in-julia-master/1298/1 "2017-01-05T09:25:21Z")

</div>

I was playing with multi-threading in Julia master and noticed that my code currently gives wrong results when more than one thread is enabled.

Julia 0.5:

```julia
julia> versioninfo()
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
  System: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, haswell)

julia> Threads.nthreads()
4

julia> function foo()
           p = Vector{Float64}(20)
           Threads.@threads for i in eachindex(p)
               p[i] = i
           end
           return p
       end
foo (generic function with 1 method)

julia> foo()
20-element Array{Float64,1}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0
 11.0
 12.0
 13.0
 14.0
 15.0
 16.0
 17.0
 18.0
 19.0
 20.0

```

Julia master:

```julia
julia> versioninfo()
Julia Version 0.6.0-dev.1924
Commit 2356ebb3f (2017-01-05 06:38 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

julia> Threads.nthreads()
4

julia> function foo()
           p = Vector{Float64}(20)
           Threads.@threads for i in eachindex(p)
               p[i] = i
           end
           return p
       end
foo (generic function with 1 method)

julia> foo()
20-element Array{Float64,1}:
 1.0
 2.0
 3.0
 4.0
 5.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

```

Also assigning `p[i] = Threads.threadid()`, like in the example of the manual, gives wrong results.

Am I doing something wrong? Is the different behavior to be expected? I checked out the manual, but didn’t find any difference in the multi-threading section.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 5, 2017, 3:24pm UTC](https://discourse.julialang.org/t/wrong-results-with-multi-threading-in-julia-master/1298/2 "2017-01-05T15:24:38Z")

</div>

Cross-reference: I opened a ticket on GitHub  
[https://github.com/JuliaLang/julia/issues/19876](https://github.com/JuliaLang/julia/issues/19876)
