# Threads.@Atomic error

**URL:** <https://discourse.julialang.org/t/threads-atomic-error/27276>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 7, 2019, 4:22pm UTC](https://discourse.julialang.org/t/threads-atomic-error/27276 "2019-08-07T16:22:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saramberr12](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@Saramberr12](https://discourse.julialang.org/u/Saramberr12)\
**Post date:** [August 7, 2019, 4:22pm UTC](https://discourse.julialang.org/t/threads-atomic-error/27276/1 "2019-08-07T16:22:03Z")

</div>

Hi all, hoping someone here can help me answer a MultiThreaded problem I am having.

Here is my code:

```julia
n = 10000000;
numprimes = Threads.Atomic{Int}(0);

function is_prime(n)
        if n == 0 | n == 1
                return 0
        elseif n == 2
                return 1
        end
        root_n = isqrt(n)
        for i = 2:root_n
                if n%i == 0
                        return 0
                end
        end
        return 1
end

function main()
        Threads.@threads for i = 1:n
                if is_prime(i) == 1
                        global numprimes = atomic_add!(numprimes, 1)
                end
        end
end

main()

```

I am getting this error when trying to use a Threads.@Atomic variable to collect `numprimes`:

```julia
Error thrown in threaded loop on thread 0: MethodError(f=typeof(Base.:(+))(), args=(Base.Threads.Atomic{Int64}(value=0), 1), world=0x00000000000063ec)Thread ID:
1

```

And I can’t find something online to help me figure out how to solve this.

Would anyone have some insight on what I should change in my code?

Many thanks!

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [August 7, 2019, 11:52pm UTC](https://discourse.julialang.org/t/threads-atomic-error/27276/2 "2019-08-07T23:52:53Z")

</div>

Remove the `global numprimes = ` from inside the loop. atomic\_add! returns the OLD value. so really you are changing numprimes to an `Int` the first time in the loop, then trying to do an atomic\_add on an `int` rather than an atomic value.

However I think the actual error you are getting is that you have multiple threads trying to update a global variable (and change it’s type) and I don’t think Julia is happy about that.

---

<div class="post-metadata">

**Author:** ![Saramberr12](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@Saramberr12](https://discourse.julialang.org/u/Saramberr12)\
**Post date:** [August 8, 2019, 1:26am UTC](https://discourse.julialang.org/t/threads-atomic-error/27276/3 "2019-08-08T01:26:07Z")

</div>

global turned out not to be the problem, global needed to be there so it could be incremented and saved/ accessed properly later.

The problem was I was calling `atomic_add!` rather than `Theads.atomic_add!`, which gives the correct output.

I also changed my variable `numprimes` at the very top to equal `0`, and I modified my `for loop` to:

```julia
function main()
        threadprimes = Threads.Atomic{Int}(0)
        Threads.@threads for i = 1:n
                if is_prime(i) == 1
                        global numprimes = Threads.atomic_add!(threadprimes, 1)
                end
        end
end

```

Thank you for your reply though! 🙂 I appreciate it.

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [August 8, 2019, 6:11pm UTC](https://discourse.julialang.org/t/threads-atomic-error/27276/4 "2019-08-08T18:11:26Z")

</div>

```julia
function main()
    threadprimes = Threads.Atomic{Int}(0)
    Threads.@threads for i = 1:n
        if is_prime(i) == 1
            Threads.atomic_add!(threadprimes, 1)
        end
    end
    global numprimes = threadprimes[]
end

```

I think that would be better. The original function has a small race condition:

1. Thread 1 increments threadprimes to 10.
2. Thread 2 increments threadprimes to 11.
3. Thread 2 sets numprimes to 11.
4. Thread 1 sets numprimes to 10.

Which you would only see if it happen at the very end, but still. 🙂

---

<div class="post-metadata">

**Author:** ![Saramberr12](https://avatars.discourse-cdn.com/v4/letter/s/6f9a4e/32.png) [@Saramberr12](https://discourse.julialang.org/u/Saramberr12)\
**Post date:** [August 8, 2019, 6:57pm UTC](https://discourse.julialang.org/t/threads-atomic-error/27276/5 "2019-08-08T18:57:59Z")

</div>

Hi pixel27,

You are right - I just modified my code and it still gives the correct output on all threads. Thank you! This is the correct answer. I’m sorry if that is what you were trying to clarify originally in your previous response, as well. 🙂
