[Haskell-cafe] semantics of concurrent program depends on -O level, -f[no-]omit-yields

Johannes Waldmann johannes.waldmann at htwk-leipzig.de
Thu Nov 29 18:42:42 UTC 2018


Dear Cafe,

I am surprised by the behaviour of the program below
(the interesting property is whether it will output "foo").

Behaviours (plural) actually: it seems to depend
on optimisation level, on omit-yields,
and on very small changes in the source code:

It does print (mostly), when compiled with -O0.
It does not, when compiled with -O2.
With -O2 -fno-omit-yields, it will print.
With -O0 -fno-omit-yields, and when I remove the two newTVar
in the beginning, it will mostly not print.

How come?

These differences already occur with the
last two lines replaced by "forever $ return ()",
so the STM stuff may be inessential here.
But that's the context where I came across the problem.

- J.W.


import Control.Concurrent.STM
import Control.Concurrent ( forkIO )
import Control.Monad ( forever )
import System.IO

main = do

  atomically $ newTVar "bar"
  atomically $ newTVar False

  forkIO $ putStrLn "foo"

  x <- atomically $ newTVar False
  forever $ atomically $ writeTVar x True



More information about the Haskell-Cafe mailing list