www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Standard output does not get flushed in cygwin?

reply Anonymouse <asdf asdf.net> writes:
Try this in a cygwin terminal:

import std.stdio;
import core.thread;

void main()
{
     foreach (i; 0..10)
     {
         writeln(i);
         Thread.sleep(1.seconds);
     }
}

This program will not output i, wait a second and then output 
i+1, etc. It will be silent for ten seconds and then spam the 
complete output in one go. If you hit Ctrl+C to break it in the 
middle of execution, nothing will be output at all.

Is there a way to work around this? The only thing I found was to 
tack stdout.flush() after every writeln call.
Jan 01 2017
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 02/01/2017 1:48 AM, Anonymouse wrote:
 Try this in a cygwin terminal:

 import std.stdio;
 import core.thread;

 void main()
 {
     foreach (i; 0..10)
     {
         writeln(i);
         Thread.sleep(1.seconds);
     }
 }

 This program will not output i, wait a second and then output i+1, etc.
 It will be silent for ten seconds and then spam the complete output in
 one go. If you hit Ctrl+C to break it in the middle of execution,
 nothing will be output at all.

 Is there a way to work around this? The only thing I found was to tack
 stdout.flush() after every writeln call.
What is your terminal emulator? Poderosa has a known problem for this. Where as ConEmu (which I have since moved over to) works fine. Fun fact, I had a similar file to the yours in /tmp/test.d :)
Jan 01 2017
parent reply Anonymouse <asdf asdf.net> writes:
On Sunday, 1 January 2017 at 13:04:30 UTC, rikki cattermole wrote:
 What is your terminal emulator?
 Poderosa has a known problem for this.
 Where as ConEmu (which I have since moved over to) works fine.

 Fun fact, I had a similar file to the yours in /tmp/test.d :)
The default mintty. I tried ConEmu now and it works with cygwin's bash shell! But then mosh and tmux break, unless I use the cygwin terminal connector[1], with which I get the same results as earlier. Maybe I'll just have to paste version (Windows) stdout.flush() everywhere. Or maybe I can hijack calls to my own writeln wrapper. [1]: https://conemu.github.io/en/CygwinMsysConnector.html
Jan 02 2017
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 2 January 2017 at 15:38:20 UTC, Anonymouse wrote:
 Maybe I'll just have to paste version (Windows) stdout.flush() 
 everywhere. Or maybe I can hijack calls to my own writeln 
 wrapper.
It comes from the C library thinking those other consoles are pipes instead of consoles, and thus block buffering instead of line buffering. Easy fix is to just flush after each line... actually, IMO, the top-level writeln should just always do this since it is one of the most common FAQs.
Jan 02 2017