Skip to content

Support multi-threaded coroutines on Kotlin/Native #462

Closed
@elizarov

Description

@elizarov
Contributor

You can have multiple threads in Kotlin/Native. Each thread can have its own event loop with runBlocking and have number of coroutines running there. Currently communication between those threads via coroutine primitives (like channels) is not supported. This issue it to track enhancement of Kotlin/Native in kotlinx.coroutines library so that all the following becomes possible:

  • Launching coroutines from one thread with a dispatcher on another thread
  • Await/join coroutine running on another thread
  • Send/Receive elements to/from coroutines on other threads

UPDATE: Currently, coroutines are supported only on the main thread. You cannot have coroutines off the main thread due to the way the library is currently structured.

UPDATE 2: the separate library version that supports Kotlin/Native multithreading is released on a regular basis.
For the details and limitations, please follow kotlin-native-sharing.md document.
The latest version: 1.5.2-native-mt

Activity

brettwillis

brettwillis commented on Aug 1, 2018

@brettwillis

Do you have a ballpark time frame for implementing this (days, weeks, months, ...)? This will help me plan how to implement the first revision of our project. Thanks!

mohit-gurumukhani

mohit-gurumukhani commented on Aug 3, 2018

@mohit-gurumukhani

Second that. Can we please get a rough estimate?

elizarov

elizarov commented on Aug 3, 2018

@elizarov
ContributorAuthor

We're in the design phase now. I'll update you on the status in couple of weeks.

Alex009

Alex009 commented on Oct 11, 2018

@Alex009

Have any progress?

elizarov

elizarov commented on Oct 11, 2018

@elizarov
ContributorAuthor

We have a work-in-progress branch in this repo with some of the code that is implemented, but it is way too complex a change, so the work there was stopped. It is hard to get it done in the current state. We've refocused our efforts on delivering high-quality single-threaded coroutines which work really well for sharing logic between Android and iOS UI apps (I highly recommend to checkout the code of KotlinConf app here https://github.com/JetBrains/kotlinconf-app). With respect to multithreading, we'll be back to drawing board to see how this story can be made easier to code with. Don't expect results soon, though.

LouisCAD

LouisCAD commented on Oct 11, 2018

@LouisCAD
Contributor
elizarov

elizarov commented on Oct 12, 2018

@elizarov
ContributorAuthor

Yes, it works without runBlocking. The only extra effort you have to make, is you have to write a trivial UI CoroutineDispatcher for iOS. We don't include it in the library yet (that's issue #470), but you can copy-and-paste code from KotlinConf app (swift version here https://github.com/JetBrains/kotlinconf-app/blob/master/konfios/konfswift/ui/UI.swift) of from discussion in #470 (Kotlin version here #470 (comment))

luca992

luca992 commented on Oct 16, 2018

@luca992

@elizarov Trying to convert Waiting for a job example to work without run blocking using (#470 (comment)) in a native macOs program. But I am still getting There is no event loop. Use runBlocking { ... } to start one. I think it probably is because my native program isn't starting NSRunLoop's mainloop. I can't quite figure it out.

I've tried starting the loop like:

fun main(args: Array<String>) {
    val job = GlobalScope.launch(MainLoopDispatcher) { // launch new coroutine and keep a reference to its Job
        delay(1000L)
        println("World!")
    }
    println("Hello,")
    NSRunLoop.currentRunLoop().runUntilDate(NSDate().dateByAddingTimeInterval(3.toDouble()))
}

But I don't think I'm doing that correctly, any ideas?

246 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @joreilly@amelin@dimsuz@kpgalligan@twyatt

        Issue actions

          Support multi-threaded coroutines on Kotlin/Native · Issue #462 · Kotlin/kotlinx.coroutines