# Concurrent Programming in Kotlin: Ensuring Thread Safety with Mutex

DevFeed: [Concurrent Programming in Kotlin: Ensuring Thread Safety with Mutex](<https://devfeed.tech/articles/concurrent-programming-in-kotlin-ensuring-thread-safety-with-mutex-23873.md>)

Original publisher: [Read original article](<https://engineering.premise.com/concurrent-programming-in-kotlin-ensuring-thread-safety-with-mutex-5d9c6b80644b?source=rss----c5fada0a103d---4>)

Author: Kwabena Bio Berko

Published: 2023-07-18T03:33:24Z

Content type: article

Language: en

Sources: [Engineering at Premise](<https://devfeed.tech/sources/engineering-at-premise-medium.md>)

Topics: [locking](<https://devfeed.tech/topics/locking.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [race-condition](<https://devfeed.tech/topics/race-condition.md>), [shared mutable state](<https://devfeed.tech/topics/shared-mutable-state.md>), [synchronization](<https://devfeed.tech/topics/synchronization.md>), [thread safe data sharing](<https://devfeed.tech/topics/thread-safe-data-sharing.md>), [multithreading](<https://devfeed.tech/topics/multithreading.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [suspend function](<https://devfeed.tech/topics/suspend-function.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-engineer](<https://devfeed.tech/tags/android-engineer.md>), [atomic](<https://devfeed.tech/tags/atomic.md>), [component](<https://devfeed.tech/tags/component.md>), [concurrency](<https://devfeed.tech/tags/concurrency.md>), [concurrent](<https://devfeed.tech/tags/concurrent.md>), [concurrent-programming](<https://devfeed.tech/tags/concurrent-programming.md>), [concurrently](<https://devfeed.tech/tags/concurrently.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [coroutines](<https://devfeed.tech/tags/coroutines.md>), [data](<https://devfeed.tech/tags/data.md>), [database](<https://devfeed.tech/tags/database.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [mobile-app-development](<https://devfeed.tech/tags/mobile-app-development.md>), [mutex](<https://devfeed.tech/tags/mutex.md>), [safety](<https://devfeed.tech/tags/safety.md>), [software](<https://devfeed.tech/tags/software.md>), [thread](<https://devfeed.tech/tags/thread.md>), [thread-safety](<https://devfeed.tech/tags/thread-safety.md>)

## AI overview

The article describes using Kotlin's Mutex to prevent concurrent refresh calls from duplicating a remote fetch and database write. It presents two behaviors: a call can check whether the Mutex is locked and skip refresh while it is in use, or use the suspending `withLock` lambda to wait until the lock is released. Restricting access to critical sections helps prevent race conditions and keep shared data consistent, but locking too much code can serialize work, reduce parallelism, and slow the application. The article recommends limiting Mutex use to sections that require exclusive access.

## Source excerpt

By Kwabena Bio Berko, Android Engineer Photo by Chris Ried on Unsplash I have recently been experimenting with mutual exclusion(Mutex), a concept in software engineering to solve race conditions when accessing shared resources. Let's assume we have a component with a refresh function that can be accessed from multiple threads or coroutines: https://medium.com/media/e19aa2a9d57bc105b841d28061a199b8/href In the implementation of this component, the function retrieves data from a remote resource and inserts it into the local database. However, these operations need to be atomic. Now, imagine if 10 threads call this function simultaneously, or even a few milliseconds apart. What happens? If you guessed that we make 10 network calls and 10 database IO operations to save the items, then you would be right, as confirmed by the below test: https://medium.com/media/9b279658dbf87021068ee21a913dc80e/hrefhttps://medium.com/media/81cdc164ad807fd45df77389c00bd4cc/href But that's not what we want. It's definitely not resource-friendly. To address this issue, we can use a Mutex. As the name suggests, a Mutex provides mutual exclusion for a specific portion of your code, imposing restrictions on the access to that portion in situations where multiple threads or coroutines may attempt to access it concurrently. In essence, Mutex allows only one thread or coroutine to work within the confines of that portion of the code at any given time. By introducing a Mutex in our refresh function, we can ensure that only one thread is executing the critical sections of the code, preventing race conditions and ensuring atomicity of the operations. https://medium.com/media/9d88a802b4c73f14ceb4d5527158af5d/href In the example above, whenever the refresh function is called, we check to see if the Mutex is already locked or been used. If not, we lock it and then perform our network call and save the results in our local database. This means that whenever another thread or coroutine calls this refresh