Hello, is anyone able to use `AtomicReference` fro...
# multiplatform
t
Hello, is anyone able to use
AtomicReference
from the native library? I do not have access to a (getter and) setter. Would I have to implement my own AtomicReference into my project to make it work?
d
Take a look at
kotlinx.atomicfu
for a multiplatform atomic reference implementation: https://github.com/Kotlin/kotlinx.atomicfu
e
otherwise yes, atomicfu allow you to reuse code across K/JVM, K/JS, and K/N (although it is different than using AtomicReference directly)
t
Isn't there a Kotlin/Native library which implements it just like java's AtomicReference or at least comes close to it?
e
that is what I linked to
t
So I will not be able to use the
AtomicReference
out of the standard library right? I don't like to have such a massive and experimental library in my project, it should be lightweight and since Kotlin decided to change the memory management, I have to use a lightweight
AtomicReference
implementation in Kotlin/Native. Kotlin/JVM has already the Java one which is good and this is what I'm aiming for in Native.
e
what do you mean?
t
Kotlin/JVM has got access to Java libraries. I'm aiming for an AtomicReference implementation like from Java or close to Java in Kotlin/Native.
a
You can easily create own expect/actual. Another solution could be to use the
utils
module from Reaktive.
e
if you have different sourcesets, you can use java.util.concurrent.AtomicReference on JVM, kotlin.native.concurrent.AtomicReference on native
t
The problem is that the Kotlin/Native AtomicReference isn't quite working. I cannot access at least a setter (without compare).
a
If you need atomics only in Kotlin/Native, then just use the K/N AtomicReference.
t
@Arkadii Ivanov look above your reply.
a
It has only
compareAndSet
and the
var value
property. Other functions have to be implemented manually as extensions.
t
So I will have to make my own implementation?
e
Copy code
fun <T> AtomicReference<T>.set(newValue: T) = whlie (!compareAndSet(value, newValue) {}
is trivial
a
This is just
ref.value = x
t
This was all I was looking for. Thanks
e
I'm not sure what the ordering guarantee is with the property, but assuming it's reasonable...
t
I thought the implementation from this library is a bit harder because I never worked with an inline variable with getter and setter provided.
r
re atomicfu, the latest roadmap has JB working on stabilizing/documenting it so I wouldn't worry as much about avoiding it anymore. https://youtrack.jetbrains.com/issue/KT-46786
a
Thanks @russhwolf, the ticket is in progress though.
e
for the record, it's always been recommended not to expose atomicfu in your public API
as an internal implementation detail, some breaking atomicfu API changes are less of a concern
a
The binary compatibility might be concerned even if used only internally.
r
right. I just mean that previously, it was sort of a JB internal thing and they weren’t talking publicly about stabilizing it at all. Now that’s changed so even if it’s not stable yet, it’s less risky to have as part of your stack
👍 1