Hello everyone. Is it possible to use kotlinx/coro...
# kotlin-native
y
Hello everyone. Is it possible to use kotlinx/coroutine with background thread in the future ? I found the convesation in
<https://github.com/Kotlin/kotlinx.coroutines/issues/462>
, but I can’t get it in detail. (Only single thread will be supported and improved ?)
t
I also have a hard time understanding this. For example when I look at the ktor mpp example: https://github.com/ktorio/ktor-samples/blob/master/mpp/client-mpp/src/commonMain/kotlin/io/ktor/samples/mpp/client/Api.kt We can launch a coroutine from the main thread to execute a http request, but dispatching one using
<http://Dispatchers.IO|Dispatchers.IO>
and waiting for the result using
async
is not possible?
y
As far as I know, No.
Kotlin/Native’s immutability is special. Unless you freeze() an object, you can’t share the object among several threads even if it is declared as
val
.
Copy code
final class Default: Kotlinx_coroutines_core_nativeCoroutineDispatcher {
    override func dispatch(context: KotlinCoroutineContext, block: Kotlinx_coroutines_core_nativeRunnable) {
        DispatchQueue.global(qos: .default).async { block.run() }
    }
}
I’m writing codes like this. It seems that when I access
block
, this code crashes.
But, it works properly with
DispatchQueue.main
instead of
global(qos: .default)
k
global(qos: .default) could give different threads. DispatchQueue.main is always UI thread.
What is subthread? thread that started by other thread?
y
sorry, I mean background thread
l
As far as I know. Currently, NO. You can not use background thread in Kotlin/Native Coroutines, It is single threaded now. But according to @elizarov , they are working on it. Hope it be implemented.
t
But that would mean that the ktor multiplatform example would execute the http call on the main thread on iOS 🤔 How I understand it is that you can use a background thread, but you can’t have a dispatcher back to the main thread.
e
But with coroutines you don’t block the main thread when you execute the http call! Coroutines enable concurrency without multithreading.
👍 1
y
@elizarov Yes, Http call with coroutines does not block main thread. But, in iOS, main queue (which manages main thread) is a serial queue. This means one processing will be done after other processing which is queued previously have done. This is a problem in case of these. - calling many http requesting at once. (should be done concurrently.) - calling http request after heavy processing like image processing.
this is an example
Copy code
DispatchQueue.main.async {
    print("1")
    sleep(10)  // this is a heavy processing
    print("2")
    sleep(10) 
}


httpCall()        // This is a function showing `hello` after receiving response. Dispatched to main queue with coroutines
print("I'm here")
async processing does not block main thread. So the output is like below.
Copy code
I'm here
1
2
hello
hello
will be printed after 20seconds later. This is a problem.
Background thread with global concurrent queue can solve this.
This is an old thread, so I’ll write it in channel 🙏