Software Transactional Memory is a promising new approach to the challenge of concurrency, as I will explain in this section. I shall explain STM using Haskell, the most beautiful programming language I know, because STM fits into Haskell particularly elegantly. If you don't know any Haskell, don't worry; we'll learn it as we go.
Here is the beginning of the code for
transfer in Haskell:
transfer :: Account -> Account -> Int -> IO ( ) -- Transfer 'amount' from account 'from' to account 'to' transfer from to amount = ...
The second line of this definition, starting with --, is a
comment. The first line gives the type signature
transfer. This signature says that
transfer takes as its arguments two values
Account (the source and
destination accounts) and an
(the amount to transfer), and returns a value of type
IO ( ). This result type says, "
transfer returns an action that, when
performed, may have some side effects, and then returns a value of type
( )." The type
), pronounced "unit," has just one value, which is also
( ); it is akin to
void in C. So,
transfer's result type
IO ( ) announces that its side effects
constitute the only reason for calling it. Before we go further, we
must explain how side effects are handled in Haskell.
A side effect is anything that reads or writes mutable state. Input/output is a prominent example of a side effect. For example, here are the signatures of ...