The challenges of atomicity violation can be solved in many ways. The first one tackles the problem in a simple fashion: since we know that the problem can only happen when a state is shared between different threads, we make sure that this never happens.
Thread confinement
What is thread confinement?
Thread confinement, as its name suggests, means confining all the coroutines accessing a shared state so that they execute on a single thread. This means that the state is no longer shared between threads: only one thread will modify the state.
This solution is great if you know that having all the coroutines modifying the state in the same thread will not negatively affect the performance of your application.
...