# Incrementing AtomicMemory{SVector}

**URL:** <https://discourse.julialang.org/t/incrementing-atomicmemory-svector/121378>\
**Category:** General Usage\
**Tags:** atomic\
**Created:** [October 16, 2024, 12:41pm UTC](https://discourse.julialang.org/t/incrementing-atomicmemory-svector/121378 "2024-10-16T12:41:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tverho](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@tverho](https://discourse.julialang.org/u/tverho)\
**Post date:** [October 16, 2024, 12:41pm UTC](https://discourse.julialang.org/t/incrementing-atomicmemory-svector/121378/1 "2024-10-16T12:41:46Z")

</div>

I’m trying to use the new `AtomicMemory in 1.11 to increment entries of a vector in an atomic way. However, the following example hangs:

```julia
using StaticArrays
mem = AtomicMemory{SVector{2,Float64}}(undef, 10)

# Initialize to zero
for j in eachindex(mem)
    Core.memoryrefset!(memoryref(mem, j), SVector(0.0, 0.0), :monotonic, true)
end

#Increment every element
for j in eachindex(mem)
    @show j
    Core.memoryrefmodify!(memoryref(mem, j), +, SVector(1.0, 1.0), :monotonic, true)
end

```

It prints out

```julia
j = 1
j = 2

```

but then hangs. It works fine when using a primitive type (Float64) instead of SVector, though. I suspect the +(SVector, SVector) operator is causing issues, but it’s hard to tell as `Core.memoryrefmodify!` doesn’t have much documentation…

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 16, 2024, 1:08pm UTC](https://discourse.julialang.org/t/incrementing-atomicmemory-svector/121378/2 "2024-10-16T13:08:15Z")

</div>

This appears to be a deadlock (which interestingly doesn’t seem to exist in 1.12 even though the code is basically the same).  
Edit: it’s especially surprising that this deadlocks given the fact that there really doesn’t look like there should be any lock contention at all…

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [October 16, 2024, 1:39pm UTC](https://discourse.julialang.org/t/incrementing-atomicmemory-svector/121378/3 "2024-10-16T13:39:55Z")

</div>

1.12 uses a `lock cmpxchg16b` instruction, whereas 1.11 resorts to calls to `jl_lock_field`/`jl_unlock_field`. Perhaps it wasn’t quite finished yet?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 16, 2024, 2:06pm UTC](https://discourse.julialang.org/t/incrementing-atomicmemory-svector/121378/4 "2024-10-16T14:06:58Z")

</div>

That part is just a result of [increase max atomic size to 16 on 64-bit platforms by vtjnash · Pull Request #42268 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/42268) increasing our max atomic size. The issue still doesn’t reproduce for objects twice as big on 1.12 where we still use a lock:

```julia
mem = AtomicMemory{NTuple{4,Float64}}(undef, 10)
for j in eachindex(mem)
    @show j
    Core.memoryrefmodify!(memoryref(mem, j), (x,y)->x.+y, (1.0, 1.0, 1.0, 1.0), :monotonic, true)
end

```
