In the previous example we saw how to manage simple counter state using atomic operations. For more complex state we can use a model similar to a mutex to safely access data across multiple threads.
In this Elm program, we create a Container type that holds a dictionary of counters. The update function handles two types of messages: Increment to increase a counter and GetCounters to retrieve the current state.
The inc function increments a named counter, and doIncrement repeatedly calls inc in a loop.
In the main function, we set up the initial state and start three tasks that increment the counters concurrently. After all increments are done, we log the final state of the counters.
Note that Elm, being a functional language, handles concurrency differently from imperative languages. Instead of using mutex locks, we use the Elm Architecture and its built-in message passing to ensure safe concurrent updates to our state.
Running this program will output the final state of the counters, which should show that “a” was incremented 20000 times and “b” 10000 times.
This example demonstrates how to manage complex state in a concurrent environment using Elm’s approach to handling side effects and state updates.