Is there multiplatform lock in Kotlin?

4ntoine :

What multiplatform Lock or synchronization approach should be used in multiplatform Kotlin code? Previously in Java code i used synchronized and i can see synchronized in Kotlin too. However it's marked as Deprecated and will be removed from common std lib soon.

I can see withLock, but it's supported on JVM only, not multiplatform.

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).

TheOperator :

From Kotlin/Native Concurrent documentation (here):

Concurrency in 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. Instead, we suggest a collection of alternative approaches, allowing you to use hardware concurrency and implement blocking 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
  • Coroutines for blocking operations (not covered in this document)

It seems that locks are not exposed in Kotlin/Native by design. There are implementations (see Lock.kt), however that class is marked internal.

However, there is a multi-platform implemenation of locks in KTOR (very limited doc, source code). It is public, but marked with @InternalApi, which may affect its stability.

You might also be interested in this KotlinLang discussion thread: Replacement for synchronized

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

kotlinOptions in kotlin multiplatform project

CPointer in Kotlin-Multiplatform

Kotlin multiplatform support for Optional

Copying to clipboard on Kotlin Multiplatform

Kotlin multiplatform project

Use JavaFX in Kotlin multiplatform

Cannot access fixedRateTimer in Kotlin Multiplatform

Hamcrest and Mockk for Kotlin Multiplatform Mobile

data classes in Kotlin multiplatform programming

Using gradle shadow with Kotlin multiplatform

http requests with Kotlin multiplatform for desktop

Service not working in Kotlin Multiplatform Application

javascript as target in kotlin multiplatform library

How to make a delay in Kotlin Multiplatform (pure kotlin)

Serve kotlin multiplatform javascript from ktor

Use Gradle sub-projects with Kotlin multiplatform

Creating/Importing Kotlin multiplatform code for iOS

Import jvm module as kotlin multiplatform dependency

Where are Swift's constants in Kotlin Multiplatform?

kotlin multiplatform: how to reduce build time?

Spring Boot Gradle dependencies in Kotlin Multiplatform

Kotlin multiplatform project is using old complied code

Kotlin Multiplatform build failure due to android target

Is there a way to use Firebase (Authentication) with Kotlin Multiplatform?

Business and Customer app in a single project in Kotlin Multiplatform?

Kotlin Multiplatform Compose + Desktop + Web + Mobile

Kotlin Multiplatform concurrent mutability iOS vs Android

Using Swift protocol default implementation in Kotlin Multiplatform

Cannot use dependencies in commonMain for kotlin multiplatform