[Haskell-cafe] STM atomic blocks in IO functions

Steffen Schuldenzucker sschuldenzucker at uni-bonn.de
Sat Jan 14 19:05:51 CET 2012

On 01/14/2012 03:55 PM, Ketil Malde wrote:
> "Bryan O'Sullivan"<bos at serpentine.com>  writes:
>>> The question is a simple one. Must all operations on a TVar happen
>>> within *the same* atomically block, or am I am I guaranteed thread
>>> safety if, say, I have a number of atomically blocks in an IO
>>> function.
>> If you want successive operations to see a consistent state, they must
>> occur in the same atomically block.
> I'm not sure I understand the question, nor the answer?  I thought the
> idea was that state should be consistent on the entry and exit of each
> "atomically" block.  So you can break your program into multiple
> transactions, but each transaction should be a semantically complete
> unit.

I think "consistent state" here means that you can be sure no other 
thread has modified a, say, TVar, within the current 'atomically' block.

E.g. for MVars, you could /not/ be sure that

   void (takeMVar mvar) >> putMVar mvar 5

won't block if mvar is full at the beginning, because a different thread 
might put to mvar between the two actions. However, in

   atomically $ void (takeTVar tvar) >> putTVar tvar 5

, this is not possible, the action after 'atomically' won't be 
influenced by any other threads while it's running, hence the name.

-- Steffen

More information about the Haskell-Cafe mailing list