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.
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
You can submit as many times as you like. Only your latest submission will be taken into account.
Sign in to test your solution.