In the previous example we saw how to manage simple counter state using atomic operations. For more complex state we can use a mutex to safely access data across multiple threads.
Running the program shows that the counters updated as expected.
Next we’ll look at implementing this same state management task using only threads and channels.
Note: In Nim, we use Lock from the locks module instead of Mutex. The concept is similar, but the syntax is slightly different. Also, Nim uses threads instead of goroutines, so we’ve adjusted the code accordingly. The defer statement in Nim works similarly to Go’s, ensuring the lock is released at the end of the function.