# Mutlithread functions within for loop

**URL:** <https://discourse.julialang.org/t/mutlithread-functions-within-for-loop/108577>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [January 9, 2024, 5:55pm UTC](https://discourse.julialang.org/t/mutlithread-functions-within-for-loop/108577 "2024-01-09T17:55:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trasor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trasor/32/213340_2.png) [@trasor](https://discourse.julialang.org/u/trasor)\
**Post date:** [January 9, 2024, 5:55pm UTC](https://discourse.julialang.org/t/mutlithread-functions-within-for-loop/108577/1 "2024-01-09T17:55:44Z")

</div>

Hello everyone, im kinda new to julia (self learning since a few weeks).

I am wondering how to use multithreading properly for functions within a for loop like:

A = … (some 3d array)  
B = …(some 3d array)

for i in 1:100  
update\_A!(A,…)  
update\_B!(B,…)  
end

in each of the update functions a 3x nested loop is performed. However, the execution order of update\_A and update\_B doesnt matter.  
I hope to improve the performance by multithread function A and B somehow.

P.S. Multi processing is no real option, since the inputs are shared and large.  
So, allocating these to another process takes too much time and memory.

Thanks in advance for any help

---

<div class="post-metadata">

**Author:** ![trasor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/trasor/32/213340_2.png) [@trasor](https://discourse.julialang.org/u/trasor)\
**Post date:** [January 9, 2024, 6:02pm UTC](https://discourse.julialang.org/t/mutlithread-functions-within-for-loop/108577/2 "2024-01-09T18:02:17Z")

</div>

mhh i guess

function update()  
@sync @Threads.threads for i in 1:1  
update\_A!(A,…)  
update\_B!(B,…)

end

for i in 1:100  
update()

end

works out.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 9, 2024, 8:13pm UTC](https://discourse.julialang.org/t/mutlithread-functions-within-for-loop/108577/3 "2024-01-09T20:13:40Z")

</div>

Well without knowing what these functions do there isn’t much we can do/say to help you. So I’ll just point you to [the Julia manual on using `@threads`](https://docs.julialang.org/en/v1/manual/multi-threading/#Using-@threads-without-data-races).

Be sure that you have no data races and avoid using `threadid()`. Also you don’t need `@sync` in front of `@threads`.

For future posts I’ll also point you to this topic 🙂

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![joaquinpelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquinpelle/32/49163_2.png) [@joaquinpelle](https://discourse.julialang.org/u/joaquinpelle)\
**Post date:** [January 9, 2024, 8:58pm UTC](https://discourse.julialang.org/t/mutlithread-functions-within-for-loop/108577/4 "2024-01-09T20:58:29Z")

</div>

For multithreading with shared memory, I recommend you to have a look at this post

> **[PSA: Thread-local state is no longer recommended](https://julialang.org/blog/2023/07/PSA-dont-use-threadid/)**
>
> PSA: Thread-local state is no longer recommended; Common misconceptions about threadid() and nthreads()

If you’d like more concrete help, I’d advise you to share a minimal working example so we can see more clearly what you have in mind.
