Does anyone have any experience with multithreadin...
# kotlin-native
s
Does anyone have any experience with multithreading on macOS and getting around all the IllegalStateExceptions? I am seeing lots of random crashes but that seems somewhat expected because I am using
TransferMode.UNSAFE
which is meant to be used as a last resort
b
I haven't heard anyone dive into the full implications of using
TransferMode.UNSAFE
. I think you're in somewhat uncharted territory
s
Yeah, I had to though because
TransferMode.SAFE
causes immediate exceptions when I pass my data to
execute
😞
s
You need to freeze your objects before passing them to workers.
s
Perhaps I need to switch to safe transfer, but it seems like that doesn’t support modifying any data that is transferred or sharing across threads
I’ve tried that (freezing) in the past many times and in many ways with the same exceptions but I can try digging into that again
b
Freezing is the simplest, but it does mean you can't do the kind of sharing it seems you want to do
s
Yeah, the shared XOR mutable restriction, while understandable for safety, is really biting me
b
there are strategies like using AtomicReference and other Atomic* classes for shared references/data across threads
s
It would be great if coroutines supported multithreading on background threads 😞
@louiscad just added macOS support too 🙂
👋 1
k
I’ve delved into the implications of UNSAFE. You absolutely should never use it if you can’t explain what the problem with using it is, and even then, basically don’t. In summary, reference counting is not atomic on thread confined state, so you get crashes because objects are deallocated but the runtime thinks they’re alive.
🎉 1