stdio line-streaming revisited

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Thu Mar 29 17:22:38 PDT 2007


Sean Kelly wrote:
> Andrei Alexandrescu (See Website For Email) wrote:
>>
>> So what's the recommended use of Cout?
>>
>> (a) If you do stdio, use Cout (and don't forget to flush Cout manually 
>> every time you plan to read from Cin).
> 
> Oh right, this is what tie() is for with iostreams.  It's come up before 
> in the past, though I can't recall if any conclusions were reached. I'll 
> admit that the current behavior is a tad confusing here though.
> 
>> (b) But not for multithreaded programs that do stdio. For those, use 
>> the logger facility. If you want multithreaded output to stdout, copy 
>> the code from 
>> http://www.dsource.org/projects/tango/wiki/ChapterLogging into your 
>> program. Be careful that that code might be incorrect; the manual 
>> doesn't specify. If it is correct, be careful with what you do inside 
>> that code, because you could deadlock yourself.
> 
> To be fair, this is mostly a documentation issue.  Though it may be that 
> the logger could be made easier to use for your example.  I'll have to 
> look at the logger in more detail before I say any more--I've only used 
> it a handful of times.
> 
>> (c) And not for programs linking with anything that uses C's stdio. 
>> For those, use Phobos.
> 
> Perhaps more accurately "programs that expect to mix output to the same 
> device as linked libraries using C's stdio."  I can't recall ever 
> needing this option, YMMV (and it obviously does).  One option might be 
> to create a CStdioCondiuit or some such that uses C library calls 
> instead of platform calls for IO.  Then the user could choose the 
> appropriate one.
> 
> I think it's also perhaps worth noting that Tango is truly intended to 
> be as much of an application framework as it is a collection of useful 
> tools.  In fact, some non-essential features have been deliberately 
> excluded to avoid discouraging third-party development.  I would also 
> personally consider C stdio integration to be a specialized need for the 
> typical D programmer, and therefore it is arguably not appropriate as 
> the default configuration for Tango's IO layer if doing so incurs design 
> or performance penalties (both of which seem to be the case, based on 
> recent experimentation).  Therefore, I am not convinced that C 
> integrated IO should be the default behavior for Tango's Cin/Cout, and 
> feel it may be better offered as a separate conduit or handled some 
> other way.  My opinion may well change if this proves to be a real issue 
> for many people, but that's my gut feeling at the moment.

I understand. Probably making the mode fast/incompatible by default is 
justifiable if people who need compatibility can opt-out and write one 
line of code (but not more!) to funnel things through the std handles 
henceforth. That is an opt-out scheme, and it looks like a decent solution.

The two other things that would be worth looking into are (a) allowing 
Cout(a, b) as a replacement for Cout(a)(b) and phasing the latter out, 
and (b) having Cin automatically call Cout.flush under certain 
conditions (probably a good heuristics: both are TTYs).


Andrei



More information about the Digitalmars-d mailing list