Why is there no such a thing as Volatile for Kotli...
# multiplatform
s
Why is there no such a thing as Volatile for Kotlin Native?
p
https://kotlinlang.org/docs/native-concurrency.html https://kotlinlang.org/docs/native-immutability.html That would be the starting point in the docs. There are also some articles and videos of talks about the memory model around that are decent. Searching “kotlin native memory model” should net you something to start with at least.
s
I’m well aware of the Kotlin Native memory model. I’m just more asking why there is no volatile in Swift and Kotlin native. It seems necessary.
p
In the swift world you generally synchronize variables with locks, semaphores, etc. There’s some atomic types https://www.swift.org/blog/swift-atomics/ It seems KM does support some atomics as well. What exactly do you need from
volatile
? I’ve honestly never declared a
volatile
var in either C or Java, since one of my professors at uni argued against them. So I wouldn’t know what you’re looking for exactly. 🤷‍♀️
p
Also, with the current “old” memory model of KM, variables get frozen anyways when handed off to another “thread”, therefore I don’t really get where volatile would help you.
s
So any time I access a variable from outside a semaphore / lock, from multiple threads, I’m in the habit of declaring it Volatile in Java, to ensure I’m not reading a cached value from a thread cache
p
You’d generally use atomics or locks on iOS / Apple platforms. But like I said, since every variable should be owned by the “thread” or is frozen, this is not an issue that you will run into?
With the new memory model I’m not entirely sure how this is handled. But maybe the blog posts on that topic give some insight.
s
I’m mostly considering this from the new memory model perspective