Control.Concurrent.STM.TMVar&#39;s  combine the best of MVars and TVars: <div><br>-Unlike TVars, they permit blocking/early retry when the TMVar is being used by other process, so that complete processes are not retried at the end when happens that the TVars have been modified in the meantime.</div>
<div>  <br>- Unlike MVars, they have no deadlocks, specially when a process is trying to adquire exclusive access to more than one TMVar.<br><br>however, It happens that fails in my windows box with ghc 6.10.1  , single core<br>
<br>here is the code and the results:<br><br><br>-----------begin code:<br><br>module Main where<br><br>import Control.Concurrent.STM<br><br>import Control.Concurrent<br>import System.IO.Unsafe<br>import GHC.Conc<br><br><br>
<br>mtxs=  unsafePerformIO $ mapM newTMVarIO $ take 5 $ repeat  0<br><br>proc i= atomically $ do<br>  unsafeIOToSTM $ putStr $ &quot;init of process &quot;++ show i++&quot;\n&quot;<br>  xs&lt;- mapM takeTMVar mtxs<br><br>
  mapM (\(mtx,x) -&gt;putTMVar mtx (x+1)) $  zip mtxs xs<br>  <br>  xs&#39; &lt;- mapM readTMVar mtxs <br>  unsafeIOToSTM $ putStr $ &quot;End of processs &quot;++show i ++ &quot; result= &quot;++ show xs&#39;++&quot;\n&quot;<br>
<br> <br>  <br>main=do<br> mapM (forkIO . proc) [1..5] <br> threadDelay 100000000<br><br></div><div>-------------end code<br><br><div>the rigth result must be (occasionally with some &quot;init of process x&quot; repeated)</div>
<div><br></div><div>init of process 1</div><div>init of process 2 </div><div>End of processs 1 result= [1,1,1,1,1]</div><div><div>init of process 2                                       -- retried</div><div>End of processs 2 result= [2,2,2,2,2]</div>
<div><div>init of process 3</div><div>End of processs 3 result= [3,3,3,3,3]</div><div><div>init of process 4</div><div>End of processs 4 result= [4,4,4,4,4]</div></div></div><div><div>init of process 5</div><div>End of processs 5 result= [5,5,5,5,5]</div>
<div><br></div><div><br></div><div>under windows the program produces strange results for example<br><br><div>init of process 1</div><div>init of process 2</div><div>init of process 3</div><div>init of process 4</div><div>
init of process 5</div><div>End of processs 1 result= [1,1,1,1,1]</div><div>End of processs 2 result= [1,1,1,1,1]</div><div>(deadlock)</div><div><br></div><div>or this other:</div><div><br></div><div><div>init of process 1</div>
<div>init of process 2</div><div>init of process 3</div><div>init of process 4</div><div>init of process 5</div><div>End of processs 1 result= [1,1,1,1,1]</div><div>End of processs 2 result= [1,1,1,1,1]</div><div>init of process 3</div>
<div>init of process 4</div><div>init of process 5</div><div>init of process 2</div><div>End of processs 4 result= [2,2,2,2,2]</div><div>End of processs 3 result= [2,2,2,2,2]</div><div>End of processs 5 result= [2,2,2,2,2]</div>
<div>End of processs 2 result= [2,2,2,2,2]</div><div>init of process 3</div><div>init of process 5</div><div>init of process 2</div><div>End of processs 3 result= [3,3,3,3,3]</div><div>End of processs 5 result= [3,3,3,3,3]</div>
</div></div></div></div></div>