Re: [Haskell-cafe] simple stm question

2011-03-14 Thread qld3303
I must've been mistaken.
Thanks

On Mon, Mar 14, 2011 at 7:05 PM, Felipe Almeida Lessa <
felipe.le...@gmail.com> wrote:

> On Mon, Mar 14, 2011 at 11:56 PM, qld3303  wrote:
> > This will work if I use atomically, but otherwise it does nothing, tchan
> > remains empty afterwards.
> > I'm not clear how this should behave.
>
> You really should use atomically on that line.  I don't see how it
> worked before.
>
> --
> Felipe.
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] simple stm question

2011-03-14 Thread Felipe Almeida Lessa
On Mon, Mar 14, 2011 at 11:56 PM, qld3303  wrote:
> This will work if I use atomically, but otherwise it does nothing, tchan
> remains empty afterwards.
> I'm not clear how this should behave.

You really should use atomically on that line.  I don't see how it
worked before.

-- 
Felipe.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] simple stm question

2011-03-14 Thread Brandon Moore
>I recently upgraded to ghc 7.0.2 from 6.12.3, along with the Haskell platform, 
>and noticed that the following code no longer works as expected:
>
>waitFor tvar = atomically $ do
>count <- readTVar tvar
>check (count == 0)
>
>worker tchan tvar = loop
>   where loop = do
>   putStrLn "checking"
>   finished <- atomically $ isEmptyTChan tchan
>   threadDelay 5
>   if finished
>  then atomically $ do val <- readTVar tvar
>   writeTVar tvar $ (subtract 1) val
>  else (atomically $ readTChan tchan) >> loop
>
>test = do
>  tchan <- newTChanIO
>  pure $ forM_ [1..5] $ writeTChan tchan -- THIS LINE 
>  tvar <- newTVarIO 1
>  forkIO $ worker tchan tvar
>  waitFor tvar
>  putStrLn "DONE"

You did not include your imports, but I assume that's Control.Applicative.pure.
If so, that line should never do anything. pure for IO is the same as return -
you are never running the STM action that line calculates. It's the
same as if you had written

>test = do
>  tchan <- newTChanIO
>  let neverGetsUsed = forM_ [1..5] $ writeTChan tchan -- THIS LINE 
>  tvar <- newTVarIO 1
>  forkIO $ worker tchan tvar
>  waitFor tvar
>  putStrLn "DONE"

Brandon



  

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] simple stm question

2011-03-14 Thread qld3303
I recently upgraded to ghc 7.0.2 from 6.12.3, along with the Haskell
platform, and noticed that the following code no longer works as expected:

waitFor tvar = atomically $ do
count <- readTVar tvar
check (count == 0)

worker tchan tvar = loop
   where loop = do
   putStrLn "checking"
   finished <- atomically $ isEmptyTChan tchan
   threadDelay 5
   if finished
  then atomically $ do val <- readTVar tvar
   writeTVar tvar $ (subtract 1) val
  else (atomically $ readTChan tchan) >> loop

test = do
  tchan <- newTChanIO
  pure $ forM_ [1..5] $ writeTChan tchan -- THIS LINE
  tvar <- newTVarIO 1
  forkIO $ worker tchan tvar
  waitFor tvar
  putStrLn "DONE"

This will work if I use atomically, but otherwise it does nothing, tchan
remains empty afterwards.
I'm not clear how this should behave.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe