3 ms·
Optimistic locking works great when what is excluded is effectively a calculation. The annoyance, though, is you have basically acknowledged that you can use a
by taeric 11mo ago
Optimistic locking works great when what is excluded is effectively a calculation. The annoyance, though, is you have basically acknowledged that you can use a compare-and-swap at the end of your calculation to know that things worked.
This is not at all always the case, though. Sometimes, what you need to use mutual exclusion for is actively working with something that is, itself, active work. That is, sometimes you have to stop something even starting.
Think about how you would model a laundry mat. It isn't enough to say you could use optimistic locks for access to the machines. You can't use the machine until you can use the machine.
This is not unheard of in computing, either. Sometimes, you can't process a buffer until you know it has been finished by the code before you.
- mrkeen 11mo ago> Think about how you would model a laundry mat. It isn't enough to say you could use optimistic locks for access to the machines. One machine? Too easy, I want multiple machines! I want to reserve this machine and that machine. And this is happening while someone else wants to grab that machine and this machine. It costs money to grab a machine, so I'll insert a coin into this machine, and then insert a coin into that machine. The other guy grabbed that machine before me, so am I locked out? It turns out he only had one coin, not two: not only did this mean he untook that machine, but the coin he spent flew out of the machine and back into his pocket. While that was going on, I took a nap - instead of busily waiting for that machines - and the laundromat operator woke me up when it was time to try to grab both machines again. myActions :: Wallets -> Machines -> STM () myActions wallets machines = do bookMachine "this machine" bookMachine "that machine" where bookMachine :: Machine -> STM () bookMachine m = do reserveMachine machines m spendCoin spendCoin :: STM () spendCoin = do decrement wallets newBalance <- getBalance wallets when (newBalance < 0) (throwSTM "Ran out of money")
- taeric 11mo agoRight, but my point is that you are still guarding a calculation, there. I suppose it is fair to say that this code just describes the paying for it side of things. My point is that I often want the code to describe everything I can actively do while the machine is in use by someone else. To that end, if I was to "script out" a trip to the laundry mat, it would not be as simple as "reserve both, when successful, pay." It would be "Confirm driers are working, find an empty washer to fill, if found fill it if not check times and plan to come back later if above a threshold start over when I'm back, otherwise pay it, come back later when it is it done, ...."