stdio performance in tango, stdlib, and perl

James Dennett jdennett at acm.org
Sat Mar 24 14:57:39 PDT 2007


Andrei Alexandrescu (See Website For Email) wrote:
> James Dennett wrote:

[snip]

>> I'm intrigued by your claim that IOStreams is not thread-safe;
>> the IOStreams framework is thread-safe in the same way that
>> the STL is thread-safe.  The one minor difference is that
>> IOStreams exposes some global variables, which is unfortunate
>> as they can easily be used in inappropriate ways in a
>> multi-threaded environment.  Then again, that is unsurprising
>> as C++ does not yet officially incorporate support for
>> multi-threading.  Is there something deeper in IOStreams that
>> you consider to be thread-unsafe, or is it just the matter of
>> its global variables?
> 
> cout << a << b;
> 
> can't guarantee that a and b will be adjacent in the output. In contrast,
> 
> printf(format, a, b);
> 
> does give that guarantee. Moreover, that guarantee is not between
> separate threads in the same process, it's between whole processes!
> Guess which of the two is usable :o).

As you appear to be saying that printf has to flush every
time it's used, I'd guess that it's unusable for performance
reasons alone.  It's also really hard to implement such a
guarantee on most platforms without using some kind of
process-shared mutex, file lock, or similar.  Does printf
really incur that kind of overhead every time something is
written to a stream, or does its implementation make use
of platform-specific knowledge on which writes are atomic
at the OS level?

Within a process, this level of safety could be achieved
with only a little (usually redundant) synchronization.
Which is useful for debugging or simplistic logging,but
not for anything else I've seen.

(IOStreams has this wrong, in different ways: it's not
just the order of output that's ill-defined if a stream
is used concurrently across multiple threads.  Nasal
demons are also possible, I hear.)

> Btw, does tango provide such a guarantee for code such as Cout(a)(b)?
> From the construct, my understanding is that it doesn't.

I'll leave that for the Tango experts to answer.

-- James



More information about the Digitalmars-d mailing list