# What's not threadsafe?

**URL:** <https://discourse.julialang.org/t/whats-not-threadsafe/10984>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 18, 2018, 9:16am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984 "2018-05-18T09:16:47Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [May 18, 2018, 9:16am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/1 "2018-05-18T09:16:47Z")

</div>

Hi all,

I am looking for a list of things that are not safe to do inside a `Threads.@threads` loop. I am aware that this is as high-level as it gets, and I am sorry about that.  
For example, I realized myself that I cannot `println` from a thread (differently to doing `println` from an `addprocs`-ed worker process) before I found [this issue](https://github.com/JuliaDiffEq/DiffEqMonteCarlo.jl/issues/17) on DiffEq. I’d love to have a list of things that don’t work - maybe here, maybe in the manual, maybe as an issue? Thanks.

---

<div class="post-metadata">

**Author:** ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)\
**Post date:** [May 18, 2018, 10:00am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/2 "2018-05-18T10:00:19Z")

</div>

One high level heuristic is that anything that does IO will not work, ie any disk reads/writes, network reads/writes or console reads/writes. ~~Also, the GC is not threadsafe yet, so you should not allocate inside a `@threads` loop.~~

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 18, 2018, 10:04am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/3 "2018-05-18T10:04:07Z")

</div>

> [@avik](#):
>
> lso, the GC is not threadsafe yet, so you should not allocate inside a `@threads` loop.

Is that really true? I think allocating inside `@threads` is fine. Do you have an issue or an example when this fail?

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [May 18, 2018, 11:14am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/4 "2018-05-18T11:14:03Z")

</div>

> [@avik](#):
>
> the GC is not threadsafe yet

At least that doesn’t match up with what was said in the recent shared memory parallelism talk, [https://youtu.be/YdiZa0Y3F3c?t=3m34s](https://youtu.be/YdiZa0Y3F3c?t=3m34s).

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 18, 2018, 11:43am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/5 "2018-05-18T11:43:18Z")

</div>

I think I’ve read the same somewhere - but are you sure this applies to allocations? I’d have thought that this just means you can’t invoke the gc explicitely 😉

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [May 18, 2018, 11:44am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/6 "2018-05-18T11:44:15Z")

</div>

gc is thread safe.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [May 18, 2018, 11:44am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/7 "2018-05-18T11:44:51Z")

</div>

No allocations at all would be quite a problem for me.

---

<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:** [May 18, 2018, 11:55am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/8 "2018-05-18T11:55:56Z")

</div>

No. Both allocation and collection are thread safe.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 18, 2018, 11:57am UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/9 "2018-05-18T11:57:06Z")

</div>

BTW `Core.println` is thread-safe.

---

<div class="post-metadata">

**Author:** ![floswald](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/floswald/32/195_2.png) [@floswald](https://discourse.julialang.org/u/floswald)\
**Post date:** [May 18, 2018, 12:01pm UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/10 "2018-05-18T12:01:18Z")

</div>

oh really? maybe I was using `info`. but i’m pretty sure that that was what killed this particular attempt.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [May 18, 2018, 12:30pm UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/11 "2018-05-18T12:30:08Z")

</div>

`Base.println` is not. (At least that is my understanding.)

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 18, 2018, 1:11pm UTC](https://discourse.julialang.org/t/whats-not-threadsafe/10984/12 "2018-05-18T13:11:34Z")

</div>

`Regex`es are not thread-safe ([StrRegex.jl](https://github.com/JuliaString/StrRegex.jl) is though! 😀)  
`BigFloat`s are not completely thread-safe (because of global state for the precision and rounding mode)  
(printing _any_ floating point (`Float16`, `Float32`, `Float64`, `BigFloat`, `Dec32`, `Dec64`, `Dec128`, etc.) or converting it to a string used to be not thread-safe, but I believe that’s fixed now (converting or printing to an `IOBuffer` at least)  
`BigInt`s also used to have the problems with converting to string and printing, but I also think that’s fixed as well.

Those are the ones that I’ve run into in Base.  
I’m sure there are a lot more in packages, any time there are some settable values (like the default precision or rounding mode) that are stored in global variables, they will not be thread-safe.
