# @threads in for loop and push! in if statement

**URL:** <https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916>\
**Category:** Performance\
**Tags:** threads, push\
**Created:** [January 19, 2025, 11:17am UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916 "2025-01-19T11:17:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discourse.julialang.org/u/Stephen)\
**Post date:** [January 19, 2025, 11:17am UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/1 "2025-01-19T11:17:55Z")

</div>

Hello there, I’m using `@threads` to accelerate my for loop, I don’t know how to `push!` a variable in `if` , I ask ChatGPT, it gives the code as following:

```julia
# Initialize an empty array to store symbols, shared across threads
lock = ReentrantLock()
low_σ = String[] # Array to store the satisfying codes

@threads for i in code
    p = get_prices(
		i, startdt = startdt, enddt = enddt, interval = "1d", 
        divsplits = true, exchange_local_time = true
	) |> DataFrame
    
    if p.close[end] < minimum(p.low) + std(p.low)
        # Use a lock to safely modify the shared array from multiple threads
        lock(lock) do
            push!(low_σ, i)
        end
    end
end

```

but it throws an error as:

```julia
ERROR: TaskFailedException

    nested task error: MethodError: objects of type ReentrantLock are not callable
    The object of type `ReentrantLock` exists, but no method is defined for this combination of argument types when trying to treat it as a callable object.

```

How to correct it? Thanks

---

<div class="post-metadata">

**Author:** ![Eliassj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eliassj/32/204117_2.png) [@Eliassj](https://discourse.julialang.org/u/Eliassj)\
**Post date:** [January 19, 2025, 12:08pm UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/2 "2025-01-19T12:08:14Z")

</div>

It would be helpful if you could post a single-threaded example of what you want to do instead of the chat-GPT answer. Would this solve your problem: [Thread-safe array building - #2 by yuyichao](https://discourse.julialang.org/t/thread-safe-array-building/3275/2) ?

---

<div class="post-metadata">

**Author:** ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discourse.julialang.org/u/Stephen)\
**Post date:** [January 19, 2025, 12:24pm UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/3 "2025-01-19T12:24:18Z")

</div>

The size of `code` is a little big (\>3000), `get_prices` fetch data from  
Yahoo Finance, I think the performance improvement using `@threads` may not be as significant, but it worth a try.

> **single-threaded example**
>
> ```julia
> using YFinance, Dates, Statistics
> code = get_all_symbols("NYSE") # 3127-element Vector{String}
> startdt = "2023-01-01"
> enddt = today()
> low_σ = String[]
> for i in code
> p = get_prices(
> i, startdt = startdt, enddt = enddt, interval = "1d", 
> divsplits = true, exchange_local_time = true
> ) |> DataFrame
>     
> if p.close[end] < minimum(p.low) + std(p.low) # filter the stock which close price is lower than it's history minimum + standard deviation of history low price
> push!(low_σ, i)
> end
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 19, 2025, 12:58pm UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/4 "2025-01-19T12:58:29Z")

</div>

Regarding the error message:  
The symbol `lock` already refers to the `lock` function exported from `Base`. In Julia you can’t have a variable named the same as an (imported) function. Changing your very first line to `l = ReentrantLock()` should make it work.

---

<div class="post-metadata">

**Author:** ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discourse.julialang.org/u/Stephen)\
**Post date:** [January 19, 2025, 1:02pm UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/5 "2025-01-19T13:02:10Z")

</div>

I have changed `lock` to `lk`, but it still gives the same error

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 19, 2025, 1:50pm UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/6 "2025-01-19T13:50:23Z")

</div>

Did you restart julia?

---

<div class="post-metadata">

**Author:** ![Stephen](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Stephen](https://discourse.julialang.org/u/Stephen)\
**Post date:** [January 20, 2025, 1:19am UTC](https://discourse.julialang.org/t/threads-in-for-loop-and-push-in-if-statement/124916/7 "2025-01-20T01:19:21Z")

</div>

Oh! I forgot to restart julia, it did works, thank you!
