这确保了互相排斥行动:
save(title, description)
这不是:
do
lock <- newMVar ()
let atomicPrint = withMVar lock . const . print
mapM_ (forkIO . atomicPrint) [['1'..'8'],['a'..'h']]
你能解释一下原因吗?根据Hackage和equational推理中withMVar和bracket_的定义,我得出结论,两段代码应该做同样的事情。但是在GHCi中运行证明我错了。
答案 0 :(得分:2)
你的订单错了;您正在获取锁定并且立即释放它;而the signature says:
bracket_
:: IO a -- computation to run first ("acquire resource")
-> IO b -- computation to run last ("release resource")
-> IO c -- computation to run in-between
-> IO c -- returns the value from the in-between computation
所以它应该是:
bracket_ (takeMVar lock) (putMVar lock ()) $ print x