@@ -0,0 +1,62 @@
1+module Try2 where
2+
3+-- import Control.Concurrent.Lifted
4+-- import Control.Concurrent.Async.Lifted
5+-- import Control.Exception.Lifted
6+import UnliftIO.Concurrent
7+import UnliftIO.Async
8+import UnliftIO.Exception
9+import Control.Concurrent.Async (AsyncCancelled(..))
10+import Debug.Trace
11+import Control.Monad
12+import Data.Functor
13+import Control.Monad.Reader
14+import Data.Maybe
15+
16+-- oneSec :: Int
17+-- oneSec = 1000000
18+
19+
20+-- main :: IO ()
21+-- main = finally
22+-- (withAsync
23+-- (catch runMonitoringThread (\AsyncCancelled -> print "---- delivered in main"))
24+-- (const $ threadDelay $ oneSec * 5))
25+-- (print "finally executed")
26+
27+
28+-- myPrint = liftIO . print
29+
30+-- runMonitoringThread :: IO ()
31+-- runMonitoringThread = do
32+-- print "something random"
33+-- runReaderT runMonitoringThread2 10
34+
35+-- runMonitoringThread2 :: ReaderT Int IO ()
36+-- runMonitoringThread2 = do
37+-- x <- ask
38+-- myPrint $ "==== got the number " <> show x
39+-- runMonitoringThread_ Nothing Nothing
40+-- where
41+-- runMonitoringThread_ mThread1 mThread2= do
42+-- thread1 <- maybe (async task1) pure mThread1
43+-- thread2 <- maybe (async task2) pure mThread2
44+-- -- catch (myWait thread1 thread2) (\AsyncCancelled -> myPrint "signal delivered in myWait" >> cancel thread1 >> cancel thread2)
45+-- finally
46+-- (myWait thread1 thread2)
47+-- (myPrint "FINALLY" >> cancel thread1 >> cancel thread2)
48+
49+-- task1 = forever $ do
50+-- myPrint "thread a is running"
51+-- threadDelay oneSec
52+
53+-- task2 = forever $ do
54+-- myPrint "thread 2 is running"
55+-- threadDelay (oneSec * 2)
56+
57+-- myWait :: Async () -> Async () -> ReaderT Int IO ()
58+-- myWait thread1 thread2 = do
59+-- waitEitherCatch thread1 thread2 >>= \case
60+-- Left _ -> runMonitoringThread_ Nothing (Just thread2)
61+-- Right _ -> runMonitoringThread_ (Just thread1) Nothing
62+-- myPrint "this should never get executed"