# Running a function with multiple threads inside @btime never ends

**URL:** <https://discourse.julialang.org/t/running-a-function-with-multiple-threads-inside-btime-never-ends/23047>\
**Category:** Julia at Scale\
**Created:** [April 11, 2019, 6:14pm UTC](https://discourse.julialang.org/t/running-a-function-with-multiple-threads-inside-btime-never-ends/23047 "2019-04-11T18:14:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [April 11, 2019, 6:14pm UTC](https://discourse.julialang.org/t/running-a-function-with-multiple-threads-inside-btime-never-ends/23047/1 "2019-04-11T18:14:21Z")

</div>

I am trying to debug an SDL application using the experimental multi-threading. It appears to conflict with a parallel for I wrote. I separated this parallel for part just to test it and benchmark separately, and I am having this problem calling the function with @btime. It specifically looks like after the first execution of the function it enters some kind of live-lock. It works OK with a single thread. Could this be a BenchmarkTools bug? Or a multi-threading bug?

```julia
using Images

using BenchmarkTools
using Base.Threads

function render_julia_set!(pixels, c::Complex{T}, maxit, cmap) where {T}
    K,J = size(pixels)
    rJ = T(1) / J
    rK = T(1) / K
    @threads for j in 1:J
        for k in 1:K
            z = (((k * rK - T(1/2)) + (j * rJ - T(1/2))im)*T(3))::Complex{T}
            ju_itrs = julia_pix(c, z, maxit)
            pixels[k, j] = cmap[ju_itrs]
        end
        # @show j
    end
    nothing
end

@inline function julia_pix(c::Complex{T}, z, maxit) where {T}
    for iteration in 1:maxit-1
        if real(z * z') >= T(4)
            return iteration
        end
        z = z^2 + c
    end
    maxit
end

function bench(maxit)
    c = (-0.75f0+0.11f0)*im

    # K=16;J=9
    K=1920;J=1080
    pixels = zeros(UInt32, K, J)

    cmap = map(colormap("Blues", maxit)) do aa
        convert(ARGB32,aa).color
    end

    @btime render_julia_set!($pixels, $c, $maxit, $cmap)
    # render_julia_set!(pixels, c, maxit, cmap)
    # @time [render_julia_set!(pixels, c, maxit, cmap) for x in 1:100]
end

bench(31)

```

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [April 11, 2019, 6:25pm UTC](https://discourse.julialang.org/t/running-a-function-with-multiple-threads-inside-btime-never-ends/23047/2 "2019-04-11T18:25:52Z")

</div>

I’ve experienced same error using @bmark or @benchmark. So just commenting for visibility.
