This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Thread safety of cout


On Fri, Oct 08, 2004 at 05:03:29AM +1300, Dan Evison wrote:
> Is it safe to have multiple threads writing to cout without any
> additional locking?
> 
> I can't seem to find a consistent message about this on the web.  This link
> seemed to suggest to me that it would be safe on linux, though perhaps I
> misread that:
> http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#9

The general rule is simple: if two threads manipulate the same,
visible object, they must control access by themselves.  The 
only things the library locks itself are invisibly-shared objects, 
such as a free-memory pool, or a string representation shared 
between two visible string objects.

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.

Nathan Myers
ncm@cantrip.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]