Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click this link to open Dodona in a new window.
Warning! The page was not fully loaded, probably because of a network issue. It could be that not all functionalities work as expected. Please try reloading the page.
MVar's met STM
Sign in to test your solution.
import Control.Concurrent.STM (STM, TVar, newTVar, readTVar, writeTVar, retry, atomically)
import Control.Concurrent (threadDelay, forkIO)
import Prelude hiding (read)
newtype STMMVAR a = STMMVAR (TVar (Maybe a))
newEmptySTMMVAR :: STM (STMMVAR a)
newEmptySTMMVAR = undefined
takeSTMMVAR :: STMMVAR a -> STM a
takeSTMMVAR (STMMVAR t) = undefined
putSTMMVAR :: STMMVAR a -> a -> STM ()
putSTMMVAR (STMMVAR t) a = undefined
write :: STMMVAR String -> STMMVAR String -> IO ()
write ta tb = do threadDelay 1000000
atomically $ do putSTMMVAR ta "Hello"
putSTMMVAR tb "World"
return ()
read :: STMMVAR String -> STMMVAR String -> IO (String, String)
read ta tb = atomically $ do b <- takeSTMMVAR tb
a <- takeSTMMVAR ta
return (a,b)
main :: IO ()
main = do ta <- atomically newEmptySTMMVAR
tb <- atomically newEmptySTMMVAR
_ <- forkIO $ write ta tb
x <- read ta tb
putStrLn $ show x