Parallel and Concurrent Programming in Haskell by Simon Marlow

Parallel and Concurrent Programming in Haskell by Simon Marlow

Author:Simon Marlow
Language: eng
Format: epub, mobi, pdf
Tags: COMPUTERS / Programming Languages / General
ISBN: 9781449335892
Publisher: O’Reilly Media
Published: 2013-07-11T16:00:00+00:00


writeChan :: Chan a -> a -> IO () writeChan (Chan _ writeVar) val = do newHole <- newEmptyMVar oldHole <- takeMVar writeVar putMVar oldHole (Item val newHole) putMVar writeVar newHole

To make the code exception-safe, our first thought might be to try this:

wrongWriteChan :: Chan a -> a -> IO () wrongWriteChan (Chan _ writeVar) val = do newHole <- newEmptyMVar modifyMVar_ writeVar $ \oldHole -> do putMVar oldHole (Item val newHole) -- return newHole --

But that doesn’t work because an asynchronous exception could strike between and . This would leave the old_hole full and writeVar pointing to it, which violates the invariants of the data structure. Hence we need to prevent that possibility too, and the simplest way is just to mask_ the whole sequence:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.