www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20215] New: redirected console app looks hang w/o .flush

https://issues.dlang.org/show_bug.cgi?id=20215

          Issue ID: 20215
           Summary: redirected console app looks hang w/o .flush
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: black80 bk.ru

-----------------------
import std;

void main() {
    for (int k=1; k<10_000; ++k) 
        // non ascii doesn't matter, just for longer string
        writeln( "hello world and привет and salut");

    "done.".writeln;

    // without it - SUBJ
    //stdout.flush;

    // without it (this app finished here) OK
    readln; 
}
-----------------------

NB: 
REDIRECTED output to file
prog.exe > file.data

output at the end looks like this:
hello world and привет and salut
hello world and привет and salut
hello world and привет and salut
hello world and 

I don't see that app is finished - no line "done."
app looks halted at some middle point.

lab:
one console runs app.
other console (FAR manager = like Night Commander) view/F3 at file.data
why so strange?
cause prints to real console is 100x slower then to file.
and viewing file/log u can stop at any interested line while output is
continue.

so look at such output my thoughts is: 
- maybe something wrong was compiled?
- maybe GC freeze for minutes? what?!

- maybe to use printf instead writeln?

possible solution (iirc Stroustrup said it at 2nd edition C++ book in 90s for
cin/cout)
when u try read data from input, flush output first.

when program prints "input numbers: " and try read nums but string is buffered
and no hints (on screen) what should user think?

so
readln should flush output first
or better is 
// automatically flush but can be manually disabled for -1 syscall
readln( term ='...', shouldFlush =true) 
// shouldFlush=false - means same. u will forget to flush

current workaround - manually flush
better solution - flush automatically for stupid and forgetful users like me

--
Sep 16 2019