question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support multi-threaded coroutines on Kotlin/Native

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:250
  • Comments:155 (66 by maintainers)

github_iconTop GitHub Comments

82reactions
qwwdfsadcommented, Sep 2, 2021

I suggest all users of native-mt builds to read the following announcement: https://blog.jetbrains.com/kotlin/2021/08/try-the-new-kotlin-native-memory-manager-development-preview/

We aim to merge the support of the new MM into coroutines 1.6.0 (https://github.com/Kotlin/kotlinx.coroutines/issues/2914) and then, depending on the stability of the new MM, promote it as the default way to write concurrent code on Native platforms.

native-mt won’t be supported as soon as the new MM is stable.

75reactions
elizarovcommented, Jun 17, 2020

📣 UPDATE: Version 1.3.7-native-mt-1.4-M2 is released — the fresh update of native-mt for Kotlin 1.4-M2. For leak-free operation, this version should be used with a specially patched version of Kotlin: 1.4-M2-eap-23 (it differs from stock 1.4-M2 in Kotlin/Native only).

This version includes fixes for additional bugs discovered by native-mt early adopters (thanks a lot for all of you ❤️):

  • Fixed awaitAll in multi-threaded scenarios (#2025)
  • Made sure that FreezingException does not break coroutine machinery (#2064)

The code is now based on the new features available in Kotlin/Native 1.4 (namely WorkerBoundReference) and there will be no more updates from native-mt branch for Kotlin 1.3. We plan to include the code from native-mt branch into the default kotlinx.coroutines distribution as Kotlin/Native version of the library for upcoming Kotlin 1.4 release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kotlin Native. Multithreading with Coroutines | by Anna Zharkova
The main idea of Kotlin Multiplatform, as well as other cross-platform SDKs, is to optimize development by writing code once and share it ......
Read more >
Concurrency and coroutines - Kotlin
A special branch of the kotlinx.coroutines library provides support for using multiple threads. It is a separate branch for the reasons listed ...
Read more >
Support multi-threaded coroutines on Kotlin/Native · Issue #462
You can have multiple threads in Kotlin/Native. Each thread can have its own event loop with runBlocking and have number of coroutines ......
Read more >
Supporting multithreaded coroutines in a Kotlin Multiplatform ...
This implementation uses just one scope which provides a CoroutineContext that makes use of the main queue from the Kotlin/Native platform ...
Read more >
Kotlin/Native multithreading using coroutines - Stack Overflow
In your original code, you are referencing a field of the parent object, which causes you to capture the whole parent and freeze...
Read more >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found