This is undoubtedly nothing more than timing issues. Remember that the main thread exiting will kill the entire process, automatically killing all other threads as side effect. So the question is how much the thread manages to get done before that happens.

If you disable output buffering, you may find that "f" or "fo" sometimes gets written before process exit.

On Thu, Nov 29, 2018 at 1:43 PM Johannes Waldmann <johannes.waldmann@htwk-leipzig.de> wrote:
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

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
brandon s allbery kf8nh
allbery.b@gmail.com