# Changing and accessing data in different threads

**URL:** https://discourse.julialang.org/t/changing-and-accessing-data-in-different-threads/71899
**Category:** General Usage
**Tags:** question
**Created:** [November 22, 2021, 4:20pm UTC](https://discourse.julialang.org/t/changing-and-accessing-data-in-different-threads/71899 "2021-11-22T16:20:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [November 22, 2021, 4:20pm UTC](https://discourse.julialang.org/t/changing-and-accessing-data-in-different-threads/71899/1 "2021-11-22T16:20:27Z")

</div>

Hi all 🙂

In the docs is written that the following is not safe.

[https://docs.julialang.org/en/v1/manual/multi-threading/#Data-race-freedom](https://docs.julialang.org/en/v1/manual/multi-threading/#Data-race-freedom)

```julia
Thread 1:
global b = false
global a = rand()
global b = true

Thread 2:
while !b; end
bad_read1(a) # it is NOT safe to access `a` here!

```

Would it be thread safe if `a` is an `Atomic`?

```julia
Thread 1:
global b = Threads.Atomic{Bool}(false)
global a = rand()
global b[] = true

Thread 2:
while !b[]; end
bad_read1(a) # would it be safe to access `a` here?

```

---

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [November 22, 2021, 4:32pm UTC](https://discourse.julialang.org/t/changing-and-accessing-data-in-different-threads/71899/2 "2021-11-22T16:32:40Z")

</div>

I believe it is necessary to use one of the `atomic_` methods documented [here](https://docs.julialang.org/en/v1/base/multi-threading/#Base.Threads.Atomic) to be safe.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 22, 2021, 4:38pm UTC](https://discourse.julialang.org/t/changing-and-accessing-data-in-different-threads/71899/3 "2021-11-22T16:38:36Z")

</div>

> [@Impressium](#):
>
> ```julia
> while !b[]; end
> bad_read1(a) # would it be safe to access `a` here?
> 
> ```

no, imagine the scheduler puts your thread to sleep right after b becomes true, and it stays asleep while any number of other threads do whatever they want with a and b… then it wakes up… b could be anything as could a.

an atomic operation is on one object, being atomic on b doesn’t mean that after you’ve checked b you can do whatever you want with any number of other variables. That’s what a lock is for.

---

<div class="post-metadata">

### Author: ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)
#### Post date: [November 22, 2021, 4:44pm UTC](https://discourse.julialang.org/t/changing-and-accessing-data-in-different-threads/71899/4 "2021-11-22T16:44:07Z")

</div>

Thank you. That makes sense!
