# Using @threads in \[f(x) for x in 1:max\_X\]

**URL:** https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871
**Category:** New to Julia
**Created:** [June 27, 2023, 5:12am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871 "2023-06-27T05:12:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 27, 2023, 5:12am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871/1 "2023-06-27T05:12:22Z")

</div>

This is a dumb example, but I would like to learn a bit more about its syntax. I was expecting that adding @threads right in front of “for” would work out to make an array.

```julia
[f(x) @threads for x in 1:max_X]
[@threads f(x) for x in 1:max_X]

```

However, none of them works.

---

<div class="post-metadata">

### Author: ![alfaromartino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfaromartino/32/52986_2.png) [@alfaromartino](https://discourse.julialang.org/u/alfaromartino)
#### Post date: [June 27, 2023, 5:23am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871/2 "2023-06-27T05:23:06Z")

</div>

As far as I know, you can’t use `Threads.@threads` with a comprehension.

You can though use a threaded map. For example,

```julia
using Distributed
pmap(f, x)

```

and it’ll apply `f` to each element of `x` in a parallel way.

Alternatively, you can use the `map` from the package `ThreadsX`.

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [June 27, 2023, 5:27am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871/3 "2023-06-27T05:27:41Z")

</div>

> [@alfaromartino](#):
>
> You can though use a threaded map. For example,
> 
> ```julia
> using Distributed
> pmap(f, x)
> 
> ```
> 
> and it’ll apply `f` to each element of `x` in a parallel way.

Note that while parallel this is not multithreaded. Instead it uses multiple worker _processes_ (which btw need to be started first).

---

<div class="post-metadata">

### Author: ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)
#### Post date: [June 27, 2023, 6:12am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871/4 "2023-06-27T06:12:06Z")

</div>

`fetch.([Threads.@spawn f(i) for i in x])` usually does the trick for me. I don’t know if it will be the most performant but it’s good enough if i quickly want to parallelize something

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [June 27, 2023, 7:00am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871/5 "2023-06-27T07:00:51Z")

</div>

There is also `ThreadsX.map`

> **[GitHub - tkf/ThreadsX.jl: Parallelized Base functions](https://github.com/tkf/ThreadsX.jl)**
>
> Parallelized Base functions. Contribute to tkf/ThreadsX.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)
#### Post date: [June 27, 2023, 9:10am UTC](https://discourse.julialang.org/t/using-threads-in-f-x-for-x-in-1-max-x/100871/6 "2023-06-27T09:10:33Z")

</div>

Thank you very much, everyone.  
I’ve decided to stick with multithreading due to the cluster system.  
ThreadsX seems very impressive!
