# Multi-threading monotonic dynamic

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…
2/ if not then how do I do that for n threads in Julia?
Thank you
Kind regards

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

No. This example illustrates it:

``````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:

``````
iter = Atomic{Int}(1)
order = Int[]
@threads for _ in 1:20
@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).

1 Like

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

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.

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.

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.