# Ktor and Kotlin/Native

DevFeed: [Ktor and Kotlin/Native](<https://devfeed.tech/articles/ktor-and-kotlin-native-25952.md>)

Original publisher: [Read original article](<https://medium.com/@kpgalligan/ktor-and-kotlin-native-fb5c06cb920a?source=rss-c2f810aa7890------2>)

Author: Kevin Galligan

Published: 2020-09-01T20:05:18Z

Content type: article

Language: en

Sources: [Stories by Kevin Galligan on Medium](<https://devfeed.tech/sources/stories-by-kevin-galligan-on-medium.md>)

Topics: [Ktor](<https://devfeed.tech/topics/ktor.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [iOS](<https://devfeed.tech/topics/ios.md>)

Tags: [bug](<https://devfeed.tech/tags/bug.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [ios](<https://devfeed.tech/tags/ios.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-multiplatform](<https://devfeed.tech/tags/kotlin-multiplatform.md>), [kotlin-native](<https://devfeed.tech/tags/kotlin-native.md>), [ktor](<https://devfeed.tech/tags/ktor.md>), [native](<https://devfeed.tech/tags/native.md>)

## AI overview

This article explains how Kotlin/Native's evolving memory model and coroutine threading model affect Ktor. It covers the initial single-threaded kotlinx.coroutines implementation, the multithreaded branch, parallel releases, and restrictions involving thread scopes and freezing.

## Source excerpt

https://commons.wikimedia.org/wiki/File:Network-cables-1.png The story of coroutines for Kotlin/Native has been complex for a while now, and with the news that the Kotlin/Native memory model will be changing, coroutines for Kotlin/Native will remain a complex story for some time. Ktor is built around coroutines. The coroutines situation fundamentally affects how Ktor functions. There have been multiple moving targets in this story, which has created a lot of confusion and a number of bug reports that kind of cross over the same core issues. In this post, I will attempt to add some clarity to the situation. The Coroutines Situation To be clear, when we're talking about "coroutines", we're generally talking about kotlinx.coroutines. It's technically a separate library, but I can't imagine a realistic situation where you'd actually use coroutines and not include kotlinx.coroutines. Initially, kotlinx.coroutines for Kotlin/Native was single-threaded. You could suspend execution, but not schedule work on different threads. This was due to Kotlin/Native's memory model. Ktor was designed to run on this single-threaded model. Also, as far as I know on iOS, you would need to initiate Ktor calls from the main thread. In late 2019, a draft PR emerged which allowed kotlinx.coroutines to communicate across threads. We'll call this the "MT" branch, for "multithreaded". From this branch, there have been parallel library releases. The current main release is '1.3.9' and the current multithreaded release is '1.3.9-native-mt'. Unless something changes, the parallel branch and release model will likely stay the same for kotlinx.coroutines until the Kotlin/Native memory model update is complete. Ktor Until 1.4, Ktor was still designed to be run with the single-threaded version of kotlinx.coroutines. You *could* run it with the MT version, but you had to be very careful to keep the scope you were in to the main thread and make sure it was never frozen. It's a little complicated to expla