www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Inconsistent stdout buffering behaviour

--000e0cd22e2afccec5048d8c84bd
Content-Type: text/plain; charset=ISO-8859-1

Greetings,

I noticed a different behaviour when using stdout.writeln() instead of
writeln() in FreeBSD.
It seems that the first issues an explicit fflush each time it's called,
while the latter doesn't.
This makes a difference at least in FreeBSD, where writes to the stdout are
line-buffered
by the system only when the destination is a real console and not, for
example, a file or a pipe.
In that case, one call will write the output one line at time, while the
other won't.

It can be easily seen with the following trivial program, if you pipe it's
output to "cat".
(using dmd 2.0.47 under FreeBSD 8.1)

module hello;

import std.stdio;
import core.sys.posix.unistd;

void main()
{
    foreach(n;0..100_000)
    {
        writeln("Hello World!");
        // stdout.writeln("Hello World!");
        sleep(1);
    }
}


I'm not sure which of the two behaviours is the right one (the C version of
the program using printf behaves like writeln, the C++ version using cout
behaves like stout.writeln...)


bye,
Marco

--000e0cd22e2afccec5048d8c84bd
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Greetings,<div><br></div><div>I noticed a different behaviour when using st=
dout.writeln() instead of writeln() in FreeBSD.</div><div>It seems that the=
 first issues an explicit fflush each time it&#39;s called, while the latte=
r doesn&#39;t.</div>
<div>This makes a difference at least in FreeBSD, where writes to the stdou=
t are line-buffered=A0</div><div>by the system only=A0when the destination =
is a real console and not, for example, a file or a pipe.</div><div>In that=
 case, one call will write the output one line at time, while the other won=
&#39;t.</div>
<div><br></div><div>It can be easily seen with the following trivial progra=
m, if you pipe it&#39;s output to &quot;cat&quot;.</div><div>(using dmd 2.0=
.47 under FreeBSD 8.1)</div><div><br></div><div><div>module hello;</div>
<div><br></div><div>import std.stdio;</div><div>import core.sys.posix.unist=
d;</div><div><br></div><div>void main()</div><div>{</div><div>=A0=A0 =A0for=
each(n;0..100_000)</div><div>=A0=A0 =A0{</div><div>=A0=A0 =A0 =A0 =A0writel=
n(&quot;Hello World!&quot;);</div>
<div>=A0=A0 =A0 =A0 =A0// stdout.writeln(&quot;Hello World!&quot;);</div><d=
iv>=A0=A0 =A0 =A0 =A0sleep(1);</div><div>=A0=A0 =A0}</div><div>}</div><div>=
<br></div><div><br></div><div>I&#39;m not sure which of the two behaviours =
is the right one (the C version of the program using printf behaves like wr=
iteln, the C++ version using cout behaves like stout.writeln...)</div>
</div><div><br></div><div><br></div><div>bye,</div><div>Marco</div><div><br=
</div>

--000e0cd22e2afccec5048d8c84bd--
Aug 11 2010