# Threading with @views and @tensor

**URL:** <https://discourse.julialang.org/t/threading-with-views-and-tensor/60466>\
**Category:** Performance\
**Created:** [May 3, 2021, 3:09pm UTC](https://discourse.julialang.org/t/threading-with-views-and-tensor/60466 "2021-05-03T15:09:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [May 3, 2021, 6:13pm UTC](https://discourse.julialang.org/t/threading-with-views-and-tensor/60466/2 "2021-05-03T18:13:35Z")

</div>

Hi,  
personally, I have never heard of views not being threadsafe but there was a recent post that mentions thread-unsafe behaviour with pre-allocated Arrays that are indexed by threadid() whenever there is a “yield” involved:

> [@Thread safe buffer correctness](https://discourse.julialang.org/t/thread-safe-buffer-correctness/60183):
>
> For a recent multi-threaded simulation, I needed a thread-safe way to use pre-allocated arrays, and the simple “nthreads() length vector of arrays” wasn’t working due to the presence of yield points in the spawn’ed tasks. (The problem noted by @tkf [here](https://juliafolds.github.io/FLoops.jl/dev/explanation/faq/#faq-state-threadid).) I came up with a fairly simple type to add some locks around the pre-allocated arrays, and it solved my problem and the simulation results look correct. However, I’m not very familiar with multi-threading, so I’m curious if there are any obvio…

[https://juliafolds.github.io/FLoops.jl/dev/explanation/faq/#faq-state-threadid](https://juliafolds.github.io/FLoops.jl/dev/explanation/faq/#faq-state-threadid)

On first glance, this doesnt seem to be the case here, but maybe there is something weird happening under the hood in the @tensor macro?  
You can find out if your issue is connected with this by allocating W inside the loop ( just for testing, of course)

Likewise, if you really are suspicious of @views causing the issue, you could temporarily remove the macro and write the indices explicitly to see if the problem ramains.

Its curious that removing the second @tensor call seems to remove thread-unsafety, it might be worth to try and write the loops over a,b,c explicitly.

hopefully one of these ideas helps you 🙂

---

_[View the full topic](https://discourse.julialang.org/t/threading-with-views-and-tensor/60466)._
