# Threads.@threads with ONE thread: how to remove the overhead?

**URL:** <https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435>\
**Category:** Performance\
**Tags:** threads\
**Created:** [April 2, 2021, 9:48am UTC](https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435 "2021-04-02T09:48:35Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [April 2, 2021, 10:25am UTC](https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435/2 "2021-04-02T10:25:43Z")

</div>

This issue was already discussed in in the thread [Overhead of `Threads.@threads`](https://discourse.julialang.org/t/overhead-of-threads-threads/53964) you linked above. I was the one starting that thread and ended up implementing my own macro `@threaded`, that checks whether only a single thread is used. If so, it just executes the code without multithreading. Otherwise, it uses `Threads.@threads`, see [introduce at-threaded macro wrapping Base at-threads by ranocha · Pull Request #426 · trixi-framework/Trixi.jl · GitHub](https://github.com/trixi-framework/Trixi.jl/pull/426). It’s not perfect but it removed some overhead for us. In particular, it removed all allocations in our use cases when using only a single thread, which is really helpful for debugging performance problems. We should probably switch to something like [ThreadingUtilities.jl](https://github.com/JuliaSIMD/ThreadingUtilities.jl), [CheapThreads.jl](https://juliasimd.github.io/CheapThreads.jl/dev/), but these packages do not have a public API yet (according to the [dev docs](https://juliasimd.github.io/ThreadingUtilities.jl/dev/public-api/) [of both](https://juliasimd.github.io/CheapThreads.jl/dev/)).

Sidenote: Your example code does not work with multiple threads since they all try to update the same `s`.

---

_[View the full topic](https://discourse.julialang.org/t/threads-threads-with-one-thread-how-to-remove-the-overhead/58435)._
