<div dir="ltr">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.<div><br></div><div>If you disable output buffering, you may find that "f" or "fo" sometimes gets written before process exit.</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Nov 29, 2018 at 1:43 PM Johannes Waldmann <<a href="mailto:johannes.waldmann@htwk-leipzig.de">johannes.waldmann@htwk-leipzig.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear Cafe,<br>
<br>
I am surprised by the behaviour of the program below<br>
(the interesting property is whether it will output "foo").<br>
<br>
Behaviours (plural) actually: it seems to depend<br>
on optimisation level, on omit-yields,<br>
and on very small changes in the source code:<br>
<br>
It does print (mostly), when compiled with -O0.<br>
It does not, when compiled with -O2.<br>
With -O2 -fno-omit-yields, it will print.<br>
With -O0 -fno-omit-yields, and when I remove the two newTVar<br>
in the beginning, it will mostly not print.<br>
<br>
How come?<br>
<br>
These differences already occur with the<br>
last two lines replaced by "forever $ return ()",<br>
so the STM stuff may be inessential here.<br>
But that's the context where I came across the problem.<br>
<br>
- J.W.<br>
<br>
<br>
import Control.Concurrent.STM<br>
import Control.Concurrent ( forkIO )<br>
import Control.Monad ( forever )<br>
import System.IO<br>
<br>
main = do<br>
<br>
atomically $ newTVar "bar"<br>
atomically $ newTVar False<br>
<br>
forkIO $ putStrLn "foo"<br>
<br>
x <- atomically $ newTVar False<br>
forever $ atomically $ writeTVar x True<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>brandon s allbery kf8nh</div><div><a href="mailto:allbery.b@gmail.com" target="_blank">allbery.b@gmail.com</a></div></div></div></div></div>