Suppose I have two states. In C, I can mutate them atomically (either all or none are mutated) using locks:
bool atomic_mutation() {
lock(state1_lock);
lock(state2_lock);
bool success = mutate1(state1);
if (!success) {
unlock(state1_lock);
unlock(state2_lock);
return false;
}
success = mutate2(state2);
if (!success) {
// revert mutate1(state1)
unlock(state1_lock);
unlock(state2_lock);
return false;
}
unlock(state1_lock);
unlock(state2_lock);
return true;
}
I’m new to Elixir. What I’ve learned is that I normally use an Agent to manage a state. But how to achieve atomicity shown above in Elixir? Thanks!
Edit: I fixed the bug in the C code above as pointed out by @al2o3cr






















