# Mysterious Core.Box

**URL:** <https://discourse.julialang.org/t/mysterious-core-box/20998>\
**Category:** New to Julia\
**Created:** [February 20, 2019, 1:32am UTC](https://discourse.julialang.org/t/mysterious-core-box/20998 "2019-02-20T01:32:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 20, 2019, 1:32am UTC](https://discourse.julialang.org/t/mysterious-core-box/20998/1 "2019-02-20T01:32:26Z")

</div>

please help. In the following, using a `@boundscheck` would cause a mysterious `Core.Box`. How to solve it??? using 1.1.0, thanks.

```julia
function fun1()
    nthreads = Threads.nthreads()

    x = zeros(200)
    Threads.@threads for t in Base.OneTo(nthreads)
        for i in t:nthreads:200
            x[i] = t + i
        end
    end

    return Statistics.mean(x)
end

function fun2()
    @boundscheck 2 >= 1 || error() # the ONLY difference

    nthreads = Threads.nthreads()

    x = zeros(200)
    Threads.@threads for t in Base.OneTo(nthreads)
        for i in t:nthreads:200
            x[i] = t + i
        end
    end

    return Statistics.mean(x)
end

julia> @code_warntype fun1() # fine

julia> @code_warntype fun2()
Body::Any
1 ── %1 = %new(Core.Box)::Core.Box

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [February 20, 2019, 3:05am UTC](https://discourse.julialang.org/t/mysterious-core-box/20998/2 "2019-02-20T03:05:04Z")

</div>

This is [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276) once again (note that the `@threads` macro creates a closure). The [`let`-block workaround](https://github.com/JuliaLang/julia/issues/15276#issuecomment-297596373) works in this case:

```julia
import Statistics
function fun2()
    @boundscheck 2 >= 1 || error() # the ONLY difference

    nthreads = Threads.nthreads()

    x = zeros(200)
    let x = x, nthreads = nthreads
        Threads.@threads for t in 1
            for i in t:nthreads:200
                x[i] = t + i
            end
        end
    end

    return Statistics.mean(x)
end

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 20, 2019, 12:00pm UTC](https://discourse.julialang.org/t/mysterious-core-box/20998/3 "2019-02-20T12:00:14Z")

</div>

It works. thanks.
