I got an ```@SharedImmutable private val backgroun...
# multiplatform
s
I got an
Copy code
@SharedImmutable
private val backgroundDispatcher: CoroutineDispatcher = Dispatchers.Default
and it looks like that every Job started by
backgroundDispatcher.launch()
runs on the same NSThread. I use
1.6.1-native-mt
. The same code on JVM runs every job on another thread. Do I have to do something extra to get this parallel?
1
m
on the
mt
branch
Dispatchers.Default
is single threaded on native, it's just a different thread than the main thread.
🙏 1
s
Oh wow, that's shocking. What can I do to have multiple threads?
I was thinking
mt
stands for *m*ulti*t*hreading 🙈
m
Use regular coroutines
1.6.1
and use the new memory model https://github.com/JetBrains/kotlin/blob/master/kotlin-native/NEW_MM.md
s
So no chance to solve this with the old model? I fear that the new model might not be stable enough. 🤔
only for evaluation purposes
... I want to ship my app in a few weeks and right now I try to tune performance.
x
s
Yes, I use the old MM already with Dispatchers.Default and I have a lot of freeze(), ensureNeverFrozen() and so on of course. I just wasn't aware that Dispatchters.Default is single-threaded, but the more I think of it the better I understand... maybe that's a default so most frameworks just work that don't care about "capture" a value. But I was thinking there must be a Multi-threaded Dispatcher I could use if I know what I do and only pass frozen state around. But looks like there is none.
x
Maybe CourotineWorker is what you want?
s
Could be. Looks like it. I will try that. Thank you. 🙂
@xiaobailong24 Almost works, except for Ktor.
x
s
Ok, thank you. So it's not just me, KTor really can't be used with CoroutineWorker. I can also confirm that for sure. But CoroutineWorker helps me anyway, because it seems to work nicely together with SQLDelight and so I can use it together with
native-mt
to load some work like metadata extraction and thumbnail creation to multiple NSThreads. I tried the normal variant (without experimental support), but that one does not work at all. So I'm now in the situation where I can't multithread anything that is related to REST API communication (since KTor doesn't work), but I can speed up some of the other things. I guess for my first release this will have to be good enough and as soon as the new memory model becomes production ready I can solve the rest of my performance issues. Again, thank you for your help.
🎉 1