Question about the multithreading usage

Hi, I have one question related to the multithreading calculation in Julia. It seems like the @threads for loop is not always safe to get what I want.

Here is one simple example of the code:
‘’’ julia
A = Any
B = Any
@threads for i in -1000:1000
a = 0.001i
b = one vector + another vector
a
push!(A, a)
push!(B, b)
end
‘’’
Then I found the length of A and B arrays are not equal to 2000. I am wondering what’s the reason for this, and how could we fix this issue. Thanks

push!() to array is not thread-safe

I see, instated of using push!(), is append safe?

no, use a Channel if you need to:

A = Channel()
B = Channel()
@threads for i in -1000:1000
    a = 0.001i
    b = one vector + another vectora
    put!(A, a)
    put!(B, b)
end

Thank you. I tested Channel(), it definitely works, but the following issue is that it is very time consuming. I am wondering how could we improve the efficiency of Channel(). Thanks

if you make Channel typed it should work better, so many try something like

A = Channel{Complex}()
B = Channel{Vector{???}}()

Thank you