I just read about KN threading model :exploding_he...
# kotlin-native
m
I just read about KN threading model 🤯 Seems like it won’t be as easy as I thought. I have an array which one thread is writing into and other thread is reading from. So I have to somehow pass it between threads
a
Basically, you should never use the same array to read and write simultaniously. It will cause problems even in java. You should either use thread-safe structures (not sure what is available in KN at the moment) or use coroutines chanalling for that.
m
I’m writing a simple emulator so the array I’m sharing is gpu memory. Emulator executes instructions on bg thread and it writes the data to gpu memory, but drawing should happen on UI thread so that’s why other thread is reading from this array. Not sure how can I do it differently. Making the array immutable means that there would be lots of allocations
d
Is it a
ByteArray
?
m
yes
d
You can either replace it with either
MutableData
or a
nativeHeap
allocation.
m
Nice! Let me try that. Thanks!
So this coroutines code I pasted above is not correct?I’m not a coroutines expert but I assumed that async {} is suspended until this internal launch {} block is finished and that’s why it works fine on jvm and android. But I may be wrong and it’s just a coincidence that it works ok
d
Oops, didn't see that. One sec.
The second snippet is a bit squint worthy, but otherwise the launch won't suspend the async.
You probably wanted
withContext
instead of
launch
.
After reading that code I think that a
DetachedObjectGraph<ByteArray>
might work better for you.
m
I’m glad I started this toy project, I’m learning a lot! 🙂
j
Doug Lea added CopyOnWriteArray{List,Set} to the jvm for lockless concurrent array access
oh woops, there is probably no COW array in native.