# Is ForwardDiff thread safe?

**URL:** <https://discourse.julialang.org/t/is-forwarddiff-thread-safe/5616>\
**Category:** General Usage\
**Created:** [August 28, 2017, 2:50pm UTC](https://discourse.julialang.org/t/is-forwarddiff-thread-safe/5616 "2017-08-28T14:50:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vgdev](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@vgdev](https://discourse.julialang.org/u/vgdev)\
**Post date:** [August 28, 2017, 2:50pm UTC](https://discourse.julialang.org/t/is-forwarddiff-thread-safe/5616/1 "2017-08-28T14:50:13Z")

</div>

Can I use @threads with `ForwardDiff.jl` ?

---

<div class="post-metadata">

**Author:** ![jrevels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jrevels/32/10393_2.png) [@jrevels](https://discourse.julialang.org/u/jrevels)\
**Post date:** [August 28, 2017, 3:25pm UTC](https://discourse.julialang.org/t/is-forwarddiff-thread-safe/5616/2 "2017-08-28T15:25:55Z")

</div>

You should be able to, yes.

Keep in mind that if you manage [your own work buffer state](http://www.juliadiff.org/ForwardDiff.jl/stable/user/api.html#Preallocating/Configuring-Work-Buffers-1), it’ll be up to you to use that memory in a thread-safe manner.

---

<div class="post-metadata">

**Author:** ![vgdev](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@vgdev](https://discourse.julialang.org/u/vgdev)\
**Post date:** [September 2, 2017, 10:36am UTC](https://discourse.julialang.org/t/is-forwarddiff-thread-safe/5616/3 "2017-09-02T10:36:51Z")

</div>

> [@jrevels](#):
>
> Keep in mind that if you manage your own work buffer state, it’ll be up to you to use that memory in a thread-safe manner.

I only need 3 gradients, so memory is not an issue. Does threadsafety also apply to reversediff?

---

<div class="post-metadata">

**Author:** ![jrevels](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jrevels/32/10393_2.png) [@jrevels](https://discourse.julialang.org/u/jrevels)\
**Post date:** [September 2, 2017, 7:10pm UTC](https://discourse.julialang.org/t/is-forwarddiff-thread-safe/5616/4 "2017-09-02T19:10:09Z")

</div>

> [@vgdev](#):
>
> Does threadsafety also apply to reversediff

Yes, ReverseDiff is also thread-safe.

EDIT: …in the sense that using ReverseDiff’s API functions within `@threads` should be fine; you may or may not run into trouble if your target function uses `@threads`, since ReverseDiff uses `push!` to push instructions to a shared tape during execution. Order of operations shouldn’t be a problem, but the nonatomicity of `push!` might cause a crash.
