Combining agents and STM
Agents by themselves are pretty useful. However, if we want to still use the agent task queuing and concurrency framework, even though an agent function needs to coordinate the state beyond the agent's own data, we'll need to use both agents and the STM: send
or send-off
to coordinate the agent's state. This will need to be combined with dosync
, ref-set
, alter
, or commute
inside the agent function to coordinate with the other state.
This combination provides simplicity over complex state and data coordination problems. This is a huge help in managing the complexity of a data processing and analysis system.
For this recipe, we'll look at the same problem we did in the Managing program complexity with agents recipe. However, this time we'll structure it a little differently. The final result will be stored in a shared reference, and the agents will update it as they go.
Getting ready
We'll need to use the same dependencies as we did for Managing program complexity with...