Hey, Can I somehow have multiple threads using cor...
# multiplatform
d
Hey, Can I somehow have multiple threads using coroutines ? I know that in the documentation they say:
The current version of kotlinx.coroutines, which can be used for iOS, supports usage only in a single thread.
but supposedly there is a multithreded version
Copy code
implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9-native-mt"){
    version {
        strictly("1.3.9-native-mt")
    }
}
I can’t seem to make it work though and in the iOS app Everything is still done on a single background thread.
Thats more less the implementation:
Copy code
suspend fun testSingleTaskOnMultipleBackgroundThread() = withContext(coroutineDispatcher) {
        val async1 = async(newSingleThreadContext("MyOwnThread")) { compute() }
        val async2 = async(newSingleThreadContext("MyOwnThread_no2")) { compute() }
        val async3 = async(newSingleThreadContext("MyOwnThread_no3")) { compute() }
        val async4 = async(newSingleThreadContext("MyOwnThread_no4")) { compute() }
        val async5 = async(newSingleThreadContext("MyOwnThread_no5")) { compute() }

        val total = async1.await() + async2.await() + async3.await() + async4.await() + async5.await()
        "four task on a background thread took $total adding them up"
    }
Copy code
fun testSingleTaskOnMultipleBackgroundThread(onComplete: (String) -> Unit) {
        scope.launch {
            val report = mutableListOf("Android parallel report:")

            val duration = measureTime {
                report.add(threadPerformance.testSingleTaskOnMultipleBackgroundThread())
            }

            report.add("four task on a background thread took $duration in parallel")
            onComplete(report.joinToString(separator = "\n"))
        }
    }
Im primarly iOS so sorry if there is some obvious mistake Im making
l
There's no multithreaded dispatcher in native for now
d
Ok can I use multiple dispatchers ?
Would that even make sense ?
l
I think it could make sense if you see a real bottleneck because you're using only on background thread.
d
I think I kinda solved it.
Copy code
suspend fun testAsyncMultithreading() = withContext(dispatcherProvider.background) {

        val async1 = async (dispatcherProvider.background) { compute() }

        val async2 = async (dispatcherProvider.background) { compute() }
        val async3 = async (dispatcherProvider.background) { compute() }
        val async4 = async (dispatcherProvider.background) { compute() }
        val async5 = async (dispatcherProvider.background) { compute() }

        val total = async1.await() + async2.await() + async3.await() + async4.await() + async5.await()
        println("four task on a background thread took $total adding them up")
    }
Copy code
actual class DispatcherProvider {
    actual val main: CoroutineDispatcher = Dispatchers.Main
    actual val background: CoroutineDispatcher
    get() =  newSingleThreadContext("BackgroundThread")
}
It actually does stuff in parallel 😄
l
You're never stopping the threads or closing the dispatchers?
I think it'd be best to write your own
async
that
finally
closes the temporary dispatcher (or use the
use
extension if it exists for this type)
d
Thats a good point. I will look into that, Thanks
l
Another possibility is to use a Worker