-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtomicState.hs
34 lines (28 loc) · 927 Bytes
/
AtomicState.hs
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
33
34
module AtomicState where
import Control.Monad
import Data.IORef
import Polysemy
import Polysemy.Async
import Polysemy.AtomicState
import Polysemy.State
incHundred :: Members '[State Int] r => Sem r ()
incHundred =
forM_ ([0 .. 99] :: [Int]) $ \_ ->
modify' @Int (+ 1)
incThousand :: Members '[State Int, Async] r => Sem r ()
incThousand =
void $ sequenceConcurrently $ map (const incHundred) ([0 .. 9] :: [Int])
incHundred' :: Members '[AtomicState Int] r => Sem r ()
incHundred' =
forM_ ([0 .. 99] :: [Int]) $ \_ ->
atomicModify' @Int (+ 1)
incThousand' :: Members '[AtomicState Int, Async] r => Sem r ()
incThousand' =
void $ sequenceConcurrently $ map (const incHundred') ([0 .. 9] :: [Int])
main :: IO ()
main = do
result <- runM $ asyncToIO $ runState @Int 0 incThousand
print result
cntr <- newIORef @Int 0
result' <- runM $ asyncToIO $ runAtomicStateIORef cntr incThousand'
print result'