# Overhead of \`Threads.@threads\`

**URL:** <https://discourse.julialang.org/t/overhead-of-threads-threads/53964>\
**Category:** Performance\
**Tags:** question, multithreading\
**Created:** [January 26, 2021, 10:04am UTC](https://discourse.julialang.org/t/overhead-of-threads-threads/53964 "2021-01-26T10:04:27Z")\
**Posts on this page:** 1\
**Showing post:** 23

<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:** [January 28, 2021, 8:43am UTC](https://discourse.julialang.org/t/overhead-of-threads-threads/53964/23 "2021-01-28T08:43:37Z")

</div>

> [@mcabbott](#):
>
> One option would be to make your own upgraded `@threads` macro, which generates both a multi-threaded and a single-threaded body.

@mcabbott Would something like the following work?

```julia
julia> module Bar

       macro threaded(expr)
           esc(quote
               if Threads.nthreads() == 1
                   $(expr)
               else
                   Threads.@threads $(expr)
               end
           end)
       end

       end

```

Using nested macros seems to be complicated in Julia, cf. [Macro hygiene is hard to use correctly in nested macro expansion · Issue #37691 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/37691). I’m not an expert in writing macros, so every input is appreciated.

---

_[View the full topic](https://discourse.julialang.org/t/overhead-of-threads-threads/53964)._
