# Threads.@threads and BitArrays

**URL:** <https://discourse.julialang.org/t/threads-threads-and-bitarrays/31878>\
**Category:** General Usage\
**Created:** [December 4, 2019, 11:07pm UTC](https://discourse.julialang.org/t/threads-threads-and-bitarrays/31878 "2019-12-04T23:07:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [December 4, 2019, 11:07pm UTC](https://discourse.julialang.org/t/threads-threads-and-bitarrays/31878/1 "2019-12-04T23:07:05Z")

</div>

Issue [https://github.com/JuliaLang/julia/issues/33750](https://github.com/JuliaLang/julia/issues/33750) is about BitArrays not being threadsafe. My reading: changing elements of a BitArray inside a `Threads.@threads for` loop can give unpredictable results.

My question is similar, but not exactly the same. In my case, the function creates a new BitArray `v` inside a `Threads.@threads for` loop as in

```julia
function f2(N)
  v = falses(N+1)
  x = zeros(Int,N,N)
  Threads.@threads for i = 1:N
    v = falses(N)
    v[i] = true
    x[v,i] .= i
  end
  return x
end

```

Calling `f2(3)` sometimes gives diagm(1:3), _sometimes not_. However, commenting out the `v = falses(N+1)` statement seems to solve the problem. Alternatively, adding `local v` also seems to solve the problem. Are these reliable approaches?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 4, 2019, 11:16pm UTC](https://discourse.julialang.org/t/threads-threads-and-bitarrays/31878/2 "2019-12-04T23:16:07Z")

</div>

This does not have anything to do with `BitArrays`. Your undefined behavior comes from the fact tthat you are sharing the variable across iterations so you sometimes get `v` created by one thread used in other threads.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [December 4, 2019, 11:22pm UTC](https://discourse.julialang.org/t/threads-threads-and-bitarrays/31878/3 "2019-12-04T23:22:46Z")

</div>

> you are sharing the variable across iterations

But, `v` is redefined inside the `Threads.@threads for` loop. Is it shared because there existed a previous `v` (defined outside the loop) and/or because it is not declared as a local?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 4, 2019, 11:54pm UTC](https://discourse.julialang.org/t/threads-threads-and-bitarrays/31878/4 "2019-12-04T23:54:59Z")

</div>

`v` is not redefined. The variable is reassigned but it is still the same variable.

I argued for making assignment local by default since the current behavior is almost never what you want in [Race condition caused by variable scope getting lifted from a multithreaded context · Issue #14948 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/14948) but Jeff disagrees.
