# Multi-threading monotonic dynamic

**URL:** https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642
**Category:** Performance
**Tags:** question, multithreading
**Created:** [September 22, 2022, 1:27pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642 "2022-09-22T13:27:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [September 22, 2022, 1:27pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/1 "2022-09-22T13:27:11Z")

</div>

Hi,  
I am trying to use multi-threading in a monotonic dynamic way (in the sense of openmp). By monotonic , I mean that if a thread exected iteration i then the thread must execute iterations larger than i subsequently.  
So for example, 2 threads and, if we have i = 1, 2 , 3 … n, then thread 1 will execute first i =1 and thread 2 will execute i = 2. Whatever thread finishs first it will execute i=3 and so on…  
1/ is it how Threads.@threads work?  
2/ if not then how do I do that for n threads in Julia?  
Thank you  
Kind regards

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [September 25, 2022, 6:11pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/2 "2022-09-25T18:11:00Z")

</div>

I was wondering whether there is maybe a more appropriate place to ask these questions?  
Thank you

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [September 25, 2022, 6:38pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/3 "2022-09-25T18:38:11Z")

</div>

> [@gitboy16](#):
>
> 1/ is it how Threads.@threads work?

No. This example illustrates it:

```julia
using .Threads
lk = ReentrantLock()
disorder = Int[]
@threads for i in 1:20
  @lock lk push!(disorder,i)
end

```

The content of array `disorder` is usually not monotonic.

Here’s one way it can be achieved:

```julia

iter = Atomic{Int}(1)
order = Int[]
@threads for _ in 1:20
  i = atomic_add!(iter,1)
  @lock lk push!(order, i)
end

```

(Not that the array `order` is guaranteed to be monotonic, some thread can be delayed, though often not in this simple example).

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [September 25, 2022, 6:58pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/4 "2022-09-25T18:58:54Z")

</div>

Thank you very much. I need to study your code because there is a lot of things I am not familiar with here.

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [September 26, 2022, 9:00am UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/5 "2022-09-26T09:00:13Z")

</div>

Am I right in saying that yhe value in `order` should be ordered from 1 to 20? I am using Julia 1.8.1 on windows and sometimes it is indeed ordered but sometimes not…especially on a fresh session.

Also if I increase the loop iteration from 20 to let’s say 600. It actually occurs more frequently.

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [September 26, 2022, 8:37pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/6 "2022-09-26T20:37:25Z")

</div>

> [@gitboy16](#):
>
> Am I right in saying that yhe value in `order` should be ordered from 1 to 20?

No. The iterations (i.e. the `i`s) are started in order, but not necessarily finished in order. (The lock-wait isn’t first-in-first-out). Within each thread there will be monotonicity, however.

---

<div class="post-metadata">

### Author: ![gitboy16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gitboy16/32/24906_2.png) [@gitboy16](https://discourse.julialang.org/u/gitboy16)
#### Post date: [September 28, 2022, 12:38pm UTC](https://discourse.julialang.org/t/multi-threading-monotonic-dynamic/87642/7 "2022-09-28T12:38:57Z")

</div>

Strange, I am probably doing something wrong then. I don’t see performance improvement I see on the C side. I will post some code eventually. For now, I will accept your answer. Thank you for your help.
