Search⌘ K
AI Features

Solution: The Problem with Shared State

Understand how to manage shared state safely in Kotlin coroutines by using mutex locks to prevent deadlocks. This lesson guides you through implementing withLock for effective concurrency control.

We'll cover the following...

Solution

The solution to the challenge we just solved is as follows.

// Write your code here

suspend fun main() {
    val mutex = Mutex()

    delay(2000)
    println("Educative")
    mutex.withLock {
        mutex.withLock {
            println("Inc.")
        }
    }
}
Solution to the challenge

Explanation

Here is a ...