# Multi-threading changing results

**URL:** <https://discourse.julialang.org/t/multi-threading-changing-results/44671>\
**Category:** New to Julia\
**Created:** [August 10, 2020, 10:16am UTC](https://discourse.julialang.org/t/multi-threading-changing-results/44671 "2020-08-10T10:16:32Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 21, 2020, 3:54am UTC](https://discourse.julialang.org/t/multi-threading-changing-results/44671/18 "2020-08-21T03:54:30Z")

</div>

FYI [FLoops.jl](https://github.com/JuliaFolds/FLoops.jl) is trying to provide a “user-friendly” interface to parallel (and sequential) loops. In particular, it can detect the error as in the OP:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/2/527f67f5fda3d547e1172f5074ea50f32fbbb40e.png)

(But I should say that I had to fix [two bugs](https://github.com/JuliaFolds/FLoops.jl/releases/tag/v0.1.2) for this example to work. FLoops.jl is a quite new package. It may find your mistakes but it may contain its own bugs 😂.)

A data race-free implementation using FLoops.jl is something like

```julia
function demo()
    Sim = randn(50,12,10,2)
    lev = zeros(10)
    for ij = 1:10
        @floop for i = 1:12
            for j = 1:50
                if Sim[j,i,ij,1] > 0.05 && Sim[j,i,ij,2] > 0.0
                    @reduce(
                        suma = 0.0 + Sim[j,i,ij,1]/Sim[j,i,ij,2],
                        counta = 0.0 + 1,
                    )
                end
            end
        end
        lev[ij] = suma/counta
    end
    Sim
end

```

---

_[View the full topic](https://discourse.julialang.org/t/multi-threading-changing-results/44671)._
