Mutexes in Erlang
In the previous example, we saw how to manage simple counter state using atomic operations. For more complex state, we can use a process to safely access data across multiple concurrent operations.
In this Erlang version, we use a separate process to manage the state of the counters. This approach is idiomatic in Erlang and provides thread-safe access to shared state without explicit locks.
The start/0
function initializes the counter process and spawns three processes to increment the counters concurrently. It then waits briefly for all operations to complete before retrieving and printing the final state of the counters.
The loop/1
function manages the state of the counters. It receives messages to either increment a counter or retrieve the current state of all counters.
The do_increment/3
function spawns a new process that sends increment messages to the counter process in a loop.
Running the program shows that the counters are updated as expected:
This example demonstrates how Erlang’s built-in concurrency model and message-passing paradigm can be used to manage shared state safely without explicit locks or mutexes.
Next, we’ll look at implementing this same state management task using only processes and message passing, which is more idiomatic in Erlang.