# A bug in thread?

**URL:** https://discourse.julialang.org/t/a-bug-in-thread/99835
**Category:** General Usage
**Tags:** question
**Created:** [June 4, 2023, 7:32am UTC](https://discourse.julialang.org/t/a-bug-in-thread/99835 "2023-06-04T07:32:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zhijie\_sun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhijie_sun/32/48713_2.png) [@zhijie\_sun](https://discourse.julialang.org/u/zhijie_sun)
#### Post date: [June 4, 2023, 7:32am UTC](https://discourse.julialang.org/t/a-bug-in-thread/99835/1 "2023-06-04T07:32:15Z")

</div>

I find if I use `Threads.threadid()` and `sleep` together, the code will give wrong result. Also give wrong result when I replace `sleep` by `lock`.  
The code shouldn’t output anything, but it did output, that is why I say give wrong result.

Below is my code and output, I run in julia 1.8.5 and by `julia --threads 4 xxx.jl`

```julia
using Base.Threads

lk = ReentrantLock()
n_thread = 4
dat = Array{Int64, 1}(undef, n_thread)
count::Int64 = 0
atom_count = Threads.Atomic{Int64}(0)

for i in 1:4
    global count = 0
    atom_count[] = 0
    fill!(dat, 0)

    Threads.@threads for j in 1:100
        thread_id = Threads.threadid()
        old_num = 0
        dat[thread_id] = 0

        for k in 1:10
            num = rand(1:100)
            dat[thread_id] = num
            old_num = num

            # lock(lk)
            # try
            # count += 1
            # finally
            # unlock(lk)
            # end
            sleep(0.001)
            if dat[thread_id] != old_num
                println("$thread_id $k")
            end
        end
        Threads.atomic_add!(atom_count, 1)
        
    end
    println("=======================")
    # while(atom_count[] < 100)
    # end
end

```

```julia
=======================
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
=======================
1 1
3 1
3 2
1 2
3 3
1 3
1 4
3 4
1 4
1 5
1 5
3 5
3 6
1 7
1 7
3 7
1 9
3 8
3 9
1 10
3 10
=======================
1 1
2 1
1 2
2 2
1 3
1 4
2 3
1 5
2 4
2 5
1 6
2 6
1 7
1 8
2 7
2 8
1 9
1 10
2 9
2 10
=======================

```

---

<div class="post-metadata">

### Author: ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)
#### Post date: [June 4, 2023, 8:13am UTC](https://discourse.julialang.org/t/a-bug-in-thread/99835/2 "2023-06-04T08:13:21Z")

</div>

This is caused by running `@threads` with the `:dynamic` scheduler, which is the default. If you change the threaded loop to `Threads.@threads :static for j in 1:100` then the problem goes away.

See the extended docs for more information (type `?? Threads.@threads` in the REPL). In particular, the docs mention this here

```julia
For example, the above conditions imply that:

    • The lock taken in an iteration must be released within the same iteration.

    • Communicating between iterations using blocking primitives like Channels
       is incorrect.

    • Write only to locations not shared across iterations (unless a lock or
       atomic operation is used).

    • The value of threadid() may change even within a single iteration.

```

And

```julia
:static scheduler creates one task per thread and divides the iterations equally
  among them, assigning each task specifically to each thread. In particular, the
  value of threadid() is guaranteed to be constant within one iteration. Specifying
  :static is an error if used from inside another @threads loop or from a thread other
  than 1.

```

---

<div class="post-metadata">

### Author: ![zhijie\_sun](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhijie_sun/32/48713_2.png) [@zhijie\_sun](https://discourse.julialang.org/u/zhijie_sun)
#### Post date: [June 4, 2023, 11:09am UTC](https://discourse.julialang.org/t/a-bug-in-thread/99835/3 "2023-06-04T11:09:40Z")

</div>

Oh, thank you!
