[Haskell-cafe] Shared/Exclusive Locks

Robert Dockins robdockins at fastmail.fm
Wed Dec 28 14:08:04 EST 2005

On Dec 28, 2005, at 1:38 PM, Tomasz Zielonka wrote:

> On Wed, Dec 28, 2005 at 05:28:28PM +0000, Chris Kuklewicz wrote:
>> But STM, wrapped in small pieces, makes for interesting IO commands
>> (untested):
>> waitForZero :: (Num a, Ord a) => (TVar a) -> IO ()
>> waitForZero tv = atomically $ do
>>   v <- readTVar tv
>>   when (v>0) retry
> This function is rather useless in IO - you will get race conditions.
> That's what you get for leaving the STM wonderland ;-)

Actually, I think it works in the particular instance given of  
constructing read-write locks because the call to waitForZero is  
guarded by a mutex.  But it would certainly be easy to introduce a  
race condition using constructs like this.  Given the alternatives  
{use STM fully, use STM some, don't use STM}, I would have a hard  
time justifying the "use STM some" alternative (at least for new  
programs).  If you are OK with introducing a dependency on STM, why  
not go whole hog?

Rob Dockins

Speak softly and drive a Sherman tank.
Laugh hard; it's a long way to the bank.
           -- TMBG

More information about the Haskell-Cafe mailing list