As mentioned in the first chapter, there is a type of concurrency error called atomicity violation. This type of error happens when the state of an object is modified concurrently without correct synchronization. The example that we saw in the first chapter was pretty simple: many coroutines modified the state of an object from different threads, and this resulted in some of those modifications being lost.
This type of error can happen with Kotlin too, but the language offers primitives that will help you to use the right design so that you avoid atomicity violation.
First, we need to truly understand what atomicity means and how violations can happen. Then we will be able to understand how to write our code to make it run atomically.