In my application several threads modify the same data. Whilst under “normal” circumstances there will always be a messages, I found out that with very large amounts of data it can happen that an other thread has been faster and deleted messages before loopAction runs.
transMit :: Socket -> POSIXTime -> String -> TPSQ -> TMap -> IO ()
transMit s time newmsgs q m = do
loopAction <- atomically $ do
mT <- readTVar m
qT <- readTVar q
let mT' = Map.delete key mT
let qT' = PSQ.delete key qT
writeTVar q (PSQ.insert key time qT')
writeTVar m (Map.insert key [newmsgs] mT')
return (let Just messages = Map.lookup key mT in sendq s (B.pack $ unwords messages) "192.168.35.84" 4711)
loopAction
I tried here a case expressions such as
case (Map.lookup key MT) of
Nothing -> return ()
_ -> something w IO
but it does not work of course since the one returns () and the other branch returns IO (), etc.. What is my best take to resolve this?
This should fix the type error:
This way both branches yield a
IO (), resulting in aSTM (IO ())overall.But I’m not sure what’s happening in terms of the values.
mTsurely can’t be modified by a different thread…