Prev Next

STM Teaser

 type Resource = TVar Int
 putR :: Resource -> Int -> STM ()
 putR r i = do { v <- readTVar r
               ; writeTVar r (v+i) }
 getR :: Resource -> Int -> STM ()
 getR r i = do { v <- readTVar r
               ; if (v < i) then retry
                 else writeTVar r (v-i) }
 atomic ( getR r x )
 atomic ( putR r x )
2