# Threads.@threads not working

**URL:** <https://discourse.julialang.org/t/threads-threads-not-working/22054>\
**Category:** General Usage\
**Created:** [March 19, 2019, 2:02pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054 "2019-03-19T14:02:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 19, 2019, 2:02pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/1 "2019-03-19T14:02:15Z")

</div>

I am running into a strange problem when I try to do multithreading over a very simple loop. I have Julia version 1.1.0 and start with 4 threads. The loop is

**N=4;**  
**Threads.@threads for i in 1:N**  
**t2(i);**  
**end**

Where the function **t2()** is

**function t2(n::Int64)**  
**id = Threads.threadid();print("\nn = “,n,” at thread ",id);**  
**while(false)**  
**a=1;**  
**end**  
**end**

But when I execute :

**include(“t.jl”);**

The execution stops at

**n = 3 at thread 3**

---

<div class="post-metadata">

**Author:** ![jeff.bezanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff.bezanson/32/48_2.png) [@jeff.bezanson](https://discourse.julialang.org/u/jeff.bezanson)\
**Post date:** [March 19, 2019, 4:25pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/2 "2019-03-19T16:25:22Z")

</div>

I/O does not yet work inside `@threads` loops. Hopefully this will be fixed very soon though.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 19, 2019, 10:45pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/3 "2019-03-19T22:45:29Z")

</div>

Thank you for that information. But by I/O, did you mean reading or writing to files ? I do not do that within the body of the function t2() that I call at each iteration.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [March 19, 2019, 10:50pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/4 "2019-03-19T22:50:09Z")

</div>

I/O includes printing.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 20, 2019, 8:04pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/5 "2019-03-20T20:04:58Z")

</div>

I would be grateful if you could suggest a way around this.  
Multithreading is a very basic necessity in applications with lot of data or with large matrices. The practice of splitting independent iterations over different threads / processor is also very common.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 20, 2019, 8:24pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/6 "2019-03-20T20:24:24Z")

</div>

Don’t `print` inside your threaded loop?

Alternatively you can use [locks](https://docs.julialang.org/en/v1/base/multi-threading/#Synchronization-Primitives-1) around your printing statements, but depending upon how big the rest of your computation is the overhead there could end up swamping your runtime.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [March 21, 2019, 10:18pm UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/7 "2019-03-21T22:18:44Z")

</div>

What you could also do is have just thread 1 do all the printing, and give each other thread an array that they can `push!` their strings into. You can then decrease lock contention by only locking between thread 1 and thread n when operating on queue n, instead of having to lock across all 1:n threads for any print operation.

Edit: Of course, if you want your prints to happen in-order, you’ll need to get fancy with things like timestamps and/or separate thread 1-only queues to keep stuff aligned.

Edit2: I think this is also the same approach that will be used in the future by PARTR, doing all IO on a single thread.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 22, 2019, 1:53am UTC](https://discourse.julialang.org/t/threads-threads-not-working/22054/8 "2019-03-22T01:53:13Z")

</div>

Thank you for the recipe. I think the problem with Threading is much deeper. I will mention it in another post.
