Poor STM

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Control.Concurrent
import Control.Concurrent.STM
import Control.Monad
import Data.Array.MArray
import System.Random
import System.CPUTime
randomModifier :: TArray Int Int -> IO ()
randomModifier a = do
bounds <- atomically $ getBounds a
i <- randomRIO bounds
x <- randomRIO (1, 100)
atomically $ writeArray a i x
threadDelay 100000
randomModifier a
atomicSum :: TArray Int Int -> IO Int
atomicSum a =
atomically $ getElems a >>= return . sum
clockMeasure :: IO a -> IO Integer
clockMeasure action = do
t1 <- getCPUTime
action
t2 <- getCPUTime
return $ t2 - t1
main = do
a <- atomically $ newArray (1, 10000) 0
forkIO $ randomModifier a
replicateM_ 100 $ do
print =<< (clockMeasure $ atomicSum a)