Thread safety of cout

Nathan Myers ncm-nospam@cantrip.org
Fri Oct 8 01:52:00 GMT 2004


On Fri, Oct 08, 2004 at 01:43:19AM +0200, Paolo Carlini wrote:
> >I would expect any program that uses cout in more than one thread,
> >without locking, to fail in random ways.  A deadlock or crash means 
> >you got lucky, because by the specs in that case it's allowed to 
> >erase your disk and impregnate your sister.
> 
> Something that I still don't grasp completely is the difference between
> fwrite / fwrite_unlocked and fflush / fflush_unlocked: the (glibc (*))
> specs don't seem to imply that the first two, respectively, take a 
> lock?!? In that case, I would exclude at least pregnancy... ;)

Probably the POSIX people who (rashly) chose to ihave putc() and getc() 
take a lock by default felt that special optimized versions of fwrite() 
and fflush() were not necessary, because they would not be called 
frequently enough for the locking overhead to matter.  I would imagine 
further that the glibc people added the "_unlocked" versions for 
what they thought of as completeness.

Nathan Myers
ncm@cantrip.org



More information about the Libstdc++ mailing list