Nice example of using Software Transactional Memory. Which is starting to be available in things like gcc, but is much much nicer in Haskell for reasons involving purity and monads.<p>Particularly this function, which changes a variable in the database, transactionally.<p><pre><code> appV :: (DB -> DB) -> TVar DB -> IO ()
appV fn x = atomically $ readTVar x >>= writeTVar x . fn
</code></pre>
It would be easy to use this to add commands that eg, increment counters in the database, and the STM would ensure that it works correctly with multiple concurrent writers. No locking needed. A quick example, which could be improved by changing the DB type to support Int as well as String values:<p><pre><code> incrCommand :: Handle -> [String] -> (TVar DB) -> IO ()
incrCommand handle k db = do
appV incvar db
hPutStrLn handle $ "OK"
where
incvar k v = insert k $ show $ (fromMaybe 0 $ readMaybe v) + 1
</code></pre>
I don't know how well Haskell's STM performs compared with eg, database transactions or locking. It's been more than fast enough for my own needs. Anybody know?