O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

With MVar, where there is no control over the subsequent options as far as atomicity is concerned. For example, reading the account balance and doing a transaction have to be done separately with MVar and not together. STM on the other hand, fully guarantees the atomicity of transactions in an STM monad.

In the preceding recipe, we use TVar (which is like MVar but is used in the context of STM). We use readTVar and writeTVar for reading and writing TVars. But all operations are enclosed in an STM monad. We have to use function atomically to run STM in the IO context.

In the transact function, we use retry in the context of STM. This tells STM to retry the same action. In this case, it will result in a blocked action until ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required