blob: 986cb66b273f8e1feb9040a7d23082d94496db41 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import Control.Exception as E
import Data.IORef
import System.Mem.Weak
import Control.Concurrent
main :: IO ()
main = do
ref <- newIORef 'x'
weak <- mkWeakIORef ref $ putStrLn "IORef finalized"
let check = deRefWeak weak >>= \m -> case m of
Nothing -> putStrLn "IORef was GCed"
Just ref' -> do
x <- readIORef ref'
putStrLn $ "IORef still alive, and contains " ++ show x
m <- newEmptyMVar
check
takeMVar m `catch` \ex -> do
putStrLn $ "caught exception: " ++ show (ex :: SomeException)
check
readIORef ref >>= print
|