[英]Is there multiplatform lock in Kotlin?
What multiplatform Lock or synchronization approach should be used in multiplatform Kotlin code?在多平台Kotlin 代码中应该使用什么多平台 Lock 或同步方法? Previously in Java code i used
synchronized
and i can see synchronized
in Kotlin too.以前在 Java 代码中我使用了
synchronized
,我也可以在 Kotlin 中看到synchronized
。 However it's marked as Deprecated and will be removed from common
std lib soon.但是它被标记为已弃用,很快就会从
common
标准库中删除。
I can see withLock
, but it's supported on JVM only, not multiplatform.我可以看到
withLock
,但它仅在JVM上受支持,在多平台上不受支持。
Any thoughts?有什么想法吗?
PS.附注。 For now we don't want to migrate to Kotlin coroutines because of too much rewrite and coroutines library footprint (too large for Android library with strict disk footprint requirements).
目前我们不想迁移到 Kotlin 协程,因为重写和协程库占用过多(对于具有严格磁盘占用空间要求的 Android 库来说太大)。
From Kotlin/Native Concurrent documentation ( here ):来自 Kotlin/Native Concurrent 文档( 这里):
Concurrency in Kotlin/Native
Kotlin/Native 中的并发
Kotlin/Native runtime doesn't encourage a classical thread-oriented concurrency model with mutually exclusive code blocks and conditional variables, as this model is known to be error-prone and unreliable.
Kotlin/Native 运行时不鼓励具有互斥代码块和条件变量的经典的面向线程的并发模型,因为已知该模型容易出错且不可靠。 Instead, we suggest a collection of alternative approaches, allowing you to use hardware concurrency and implement blocking IO.
相反,我们建议使用一系列替代方法,允许您使用硬件并发并实现阻塞 IO。 Those approaches are as follows, and they will be elaborated on in further sections:
这些方法如下,它们将在进一步的部分中详细说明:
- Workers with message passing
具有消息传递功能的工人
- Object subgraph ownership transfer
对象子图所有权转移
- Object subgraph freezing
对象子图冻结
- Object subgraph detachment
对象子图分离
- Raw shared memory using C globals
使用 C 全局变量的原始共享内存
- Coroutines for blocking operations (not covered in this document)
用于阻塞操作的协程(本文档未涵盖)
It seems that locks are not exposed in Kotlin/Native by design. Kotlin/Native 的设计似乎没有公开锁。 There are implementations (see Lock.kt ), however that class is marked
internal
.有一些实现(参见Lock.kt ),但是该类被标记为
internal
。
However , there is a multi-platform implemenation of locks in KTOR (very limited doc , source code ).但是,在 KTOR 中有一个多平台的锁实现(非常有限的文档, 源代码)。 It is public, but marked with
@InternalApi
, which may affect its stability.它是公开的,但标有
@InternalApi
,这可能会影响其稳定性。
You might also be interested in this KotlinLang discussion thread: Replacement for synchronized您可能还对 KotlinLang 讨论主题感兴趣: Replacement for synchronized
There is no lock nor synchronized in Kotlin common. Kotlin common 中没有锁也没有同步。 Kotlin's approach is to use immutable data.
Kotlin 的方法是使用不可变数据。 You can add your own expect AtomicReference in common and actual implementations in JVM Native, it will help a lot.
您可以在 JVM Native 中的常见和实际实现中添加您自己的期望 AtomicReference,它将有很大帮助。 Also keep in mind that coroutines in Native are single threaded at the moment.
还要记住,Native 中的协程目前是单线程的。 Also you can't share mutable state between threads in Native.
此外,您不能在 Native 的线程之间共享可变状态。
There is full "lock" multi-platform implementation in Kotlin coroutines library. Kotlin 协程库中有完整的“锁定”多平台实现。 It is based on atomicfu and I think that can be easily extracted from there, even if you really don't want to depend from the full coroutine library:
它基于 atomicfu,我认为可以从那里轻松提取,即使您真的不想依赖完整的协程库:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.