Kotlin Mutex
Protect the entire check-and-update operation when coroutines share state.
How it works
A coroutine Mutex provides suspending mutual exclusion. Place the condition and mutation in the same withLock block. The protected function must not leak an unprotected mutable reference. A mutex protects one in-memory object, not records in another process or database.
Run this Kotlin example
These are local Kotlin/JVM programs. Install a JDK and the compiler using the official Kotlin compiler guide, or use an IDE with Kotlin support. Check java -version and kotlinc -version in a new terminal. The reviewed compiler is Kotlin 2.4.21 with a JVM 21 target, executed on Temurin JDK 25.0.5. Use JDK 21 or newer for this target. Android, Kotlin/JS and Kotlin/Native require separate platform projects and are not the tested environment here.
Save the complete source as mutex.kt in its own learning folder. The package line gives this example a namespace, not a network address. Bhylu has no Kotlin browser runner; do not use the JavaScript or Python editor. Apache, PHP and MySQL are not required.
This example also needs kotlinx-coroutines-core-jvm-1.11.0.jar, the separately versioned JetBrains coroutines library. Obtain that exact artifact from Maven Central, retain its licence and put it beside this source. Merely installing the compiler does not promise that its bundled coroutine JAR has the reviewed version.
On Windows CMD or PowerShell, compile with kotlinc mutex.kt -jvm-target 21 -classpath kotlinx-coroutines-core-jvm-1.11.0.jar -include-runtime -d lesson.jar, then run java -cp "lesson.jar;kotlinx-coroutines-core-jvm-1.11.0.jar" bhylu.lessons.k_mutex.MutexKt. On macOS/Linux use a colon instead of the semicolon between classpath entries. java -jar alone will not add this external library.
Download the complete mutex.kt · Download all source files with hash checks
For the optional collection, save download-examples.mjs in a new learning folder and run node download-examples.mjs. A local preview uses node download-examples.mjs http://127.0.0.1:8000/examples/kotlin/. It refuses an existing bhylu-kotlin-examples folder and installs or executes no Kotlin code. Compile one ordinary lesson at a time by filename.
Example
package bhylu.lessons.k_mutex
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.launch
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
class SharedDesk(private val capacity: Int) {
init { require(capacity >= 0) }
private val mutex = Mutex()
private var used = 0
suspend fun hireOne(): Boolean = mutex.withLock {
if (used >= capacity) false else { used++; true }
}
suspend fun hired(): Int = mutex.withLock { used }
}
fun main() {
runBlocking {
val desk = SharedDesk(5)
val results = BooleanArray(20)
val jobs = List(20) { index -> launch(Dispatchers.Default) { results[index] = desk.hireOne() } }
jobs.forEach { it.join() }
println(results.count { it })
println(desk.hired())
}
}
Expected result
5 5
Each worker writes one distinct result slot, and the owner reads them only after joining all jobs. The desk updates and reads its internal count under the same lock. Exactly five requests fit.
For exact language and library contracts, consult the official Kotlin documentation. These fictional fixtures are learning programs, not a deployed hire service.