https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
s

serebit

08/29/2019, 6:07 PM
What's the best option for controlling shared mutable state in common code? I have a cache that can be accessed and updated by multiple coroutines (on JVM, anyway). Actors are unavailable on native, so should I go with a Mutex approach, or make the cache an AtomicReference?
a

Arkadii Ivanov

08/29/2019, 6:12 PM
Mutex won't help as you can't share mutable state across threads, even if access is properly synchronized. Go with AtomicReferenc + freeze + immutable structures.
b

basher

08/29/2019, 6:21 PM
stately has frozen mutable structures
k

kpgalligan

08/29/2019, 6:36 PM
You can also keep all of your data in a single thread, by way of a worker, and communicate through messages. It feels like a weird way to do it, but it’ll work
Stately collections work, but pending the outcome of relaxed mode, they need a rewrite. Performance is so so
s

serebit

08/29/2019, 6:39 PM
Could I have the cache be thread-local and contain mutable data, and have other threads communicate with it via messages with immutable data? I'm not very familiar with the memory model of native.
k

kpgalligan

08/29/2019, 6:39 PM
Yeah, that’s essentially what I’m proposing. You can keep frozen data in a non-frozen container. That’s not going to be an issue. Just don’t freeze the container :-)
s

serebit

08/29/2019, 6:40 PM
I also know that threading behavior of coroutines in K/N is going to be revamped significantly in the coming months, so I'm more concerned with race conditions on JVM than with threading on Native, at least for now.
That reminds me, is data frozen by default when crossing threads, or do I have to freeze it myself? Because having to freeze it myself would make writing this system in common code a lot more difficult.
k

kpgalligan

08/29/2019, 6:41 PM
Valid concern
b

basher

08/29/2019, 6:42 PM
you have to freeze it yourself (
.freeze()
)
a

Arkadii Ivanov

08/29/2019, 6:43 PM
But you can freeze it inside your cache class. So clients won't care about it.
k

kpgalligan

08/29/2019, 6:44 PM
"That reminds me, is data frozen by default when crossing threads, or do I have to freeze it myself? Because having to freeze it myself would make writing this system in common code a lot more difficult." Not with stately! There's a common definition for freeze that is a noop on jvm and js
s

serebit

08/29/2019, 6:47 PM
Cool (no pun intended). Thanks for the info!
k

Kris Wong

08/29/2019, 9:56 PM
have you seen calling
freeze
on an object deadlock?
k

kpgalligan

08/29/2019, 9:57 PM
No?
Crash or just stop?
k

Kris Wong

08/29/2019, 9:57 PM
i guess that makes me special 😛
k

kpgalligan

08/29/2019, 9:57 PM
Example?
k

Kris Wong

08/29/2019, 9:58 PM
well, in unit tests it appears to be crashing. from an iOS app unit test it's getting stuck.
so, both
k

kpgalligan

08/29/2019, 9:59 PM
Do you have a public example? I’ve been doing this for a while and have never seen that. I think a lot of people would be very interested in seeing that happen
1
k

Kris Wong

08/29/2019, 9:59 PM
unfortunately I do not
k

kpgalligan

08/29/2019, 10:00 PM
Can you write an example of a block of code you think is deadlocking? I will try it now. Is it always, or just sometimes?
k

Kris Wong

08/29/2019, 10:01 PM
always.
b

basher

08/29/2019, 10:01 PM
are you calling it on a background thread not managed by K/N?
i could see something weird happening if you forgot to
initRuntimeIfNeeded()
k

kpgalligan

08/29/2019, 10:01 PM
I mean, I’ve never seen this, and have done quite a bit of both testing and calling freeze.
An example piece of code would be helpful
k

Kris Wong

08/29/2019, 10:02 PM
could be, XCTest is managing the threads. let me explore a little more in the debugger
no, it's the main thread
I added a method to a class,
makeImmutable
, which just calls freeze. unfortunately I can't step into that.
let me try App Code
k

kpgalligan

08/29/2019, 10:06 PM
Kotlin xcode plugin
s

serebit

08/29/2019, 10:07 PM
This isn't really related to my original post, can you guys move this to a new thread?
1
b

basher

08/29/2019, 10:07 PM
Can just DM Kevin, and I'll find out what happened later 🙂
k

Kris Wong

08/29/2019, 10:07 PM
hah
5 Views