This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: Thread safety of cout
- From: Nathan Myers <ncm-nospam at cantrip dot org>
- To: libstdc++ at gcc dot gnu dot org
- Date: Thu, 7 Oct 2004 14:41:28 -0700
- Subject: Re: Thread safety of cout
- References: <047501c4ac87$2fcb1fa0$0c01a8c0@atlantic>
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