# Atomic\_min for floats

**URL:** <https://discourse.julialang.org/t/atomic-min-for-floats/35880>\
**Category:** General Usage\
**Tags:** question, multithreading\
**Created:** [March 12, 2020, 1:57pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880 "2020-03-12T13:57:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [March 12, 2020, 1:57pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/1 "2020-03-12T13:57:08Z")

</div>

I’m getting unexpected behavior for` atomic_min!(x::Atomic{T}, val::T)` with floats. This function returns the old value of x. Works fine for Ints, but for Floats it returns the new value:

```julia
julia> E = Atomic{Float64}(10)
Atomic{Float64}(10.0)

julia> atomic_min!(E, 3.0)
3.0

julia> E
Atomic{Float64}(3.0)

```

If I change the code to use `Int64` then `atomic_min()` returns the old value (10). What is going on?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 12, 2020, 3:13pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/2 "2020-03-12T15:13:54Z")

</div>

Looks like this line should end in `return old` in order to be consistent with the documentation:

> <https://github.com/JuliaLang/julia/blob/8be0b7991596ddc6e8cdc19f1897b2d7addbe511/base/atomics.jl#L425>

Edit: Of course `atomic_add!` and `atomic_sub!` should still return `new`, so the fix will be more than three bytes.

---

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [March 18, 2020, 1:56pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/3 "2020-03-18T13:56:00Z")

</div>

Thanks. I wonder why the atomic functions return values and not booleans? The way it is now I have to compare the return value with x to see it the value of x changed, which could produce a race condition if x is written to by another thread.

---

<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:** [March 18, 2020, 2:20pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/4 "2020-03-18T14:20:34Z")

</div>

I think it returns the old value to AVOID a race condition. If you atomically retrieve the value then atomically change the value, another thread might have changed it in between those two calls. If the method returns the OLD value, then you know what value was replaced and you can do whatever checks you need to do.

---

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [March 18, 2020, 2:45pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/5 "2020-03-18T14:45:29Z")

</div>

But if the function returned a boolean then I would know whether the value was replaced _without_ having to do any checks.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 18, 2020, 3:17pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/6 "2020-03-18T15:17:45Z")

</div>

But in that case you won’t know what the old value is. Also, @pixel27 was just arguing that there’s no race which was what you claimed…

I also don’t think there’s any difference in the actual machine code so it’s only a question of who write that comparison.

---

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [March 18, 2020, 6:10pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/7 "2020-03-18T18:10:44Z")

</div>

For sure the machine code is the same. It’s just a matter of choosing a higher level API, and there probably isn’t one solution for all cases. I’m used to the GCC atomic builtin functions, such as \_\_sync\_bool\_compare\_and\_swap

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 18, 2020, 8:13pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/8 "2020-03-18T20:13:05Z")

</div>

Right it’s not about race or not. You can get the answer you want without a race.

And the current one is the one solution for all cases since the other one can be implemented using this, but not the other way around…

And fwiw, there’s \_\_sync\_val\_compare\_and\_swap and pretty much all the other sync built-in returns the value instead of a boolean. It doesn’t even make much sense to return a boolean anyway since this operation, unlike CAS, is basically always a write operation anyway…

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [April 1, 2020, 12:41pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/9 "2020-04-01T12:41:26Z")

</div>

Did anybody open an issue about this, or should I do it?

---

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [April 16, 2020, 1:25pm UTC](https://discourse.julialang.org/t/atomic-min-for-floats/35880/10 "2020-04-16T13:25:57Z")

</div>

It looks like no one’s opened an issue on this. I can do it.
