Subj : Re: Is std::cerr thread safe To : comp.programming.threads From : Joe Seigh Date : Wed Jul 20 2005 04:33 pm Sean Kelly wrote: > The STL is guaranteed to be thread-safe, but thread safety in this case > just means that it won't crash horribly if you use it in a > multi-threaded program. In my experience, writing to std::cerr from > multiple threads works just fine, but the elements may be intermixed if > writes are simultaneous. A fairly cheap way around this is to do every > write into a stringstream and then dump it en masse into the error > stream: > > std::ostringstream ostr; > ostr << "error: " << errno << '\n'; > std::cerr << ostr.rdbuf(); > > The preferable alternative would be to wrap writes in a critical > section or create your own error stream that does something like using > thread local storage for its buffer and only flushes when explicitly > instructed to do so I see what was happening to the OP. Using cerr that way is equivalent to mulitple invocations of unbuffered fprintf, e.g. fprintf(stderr, "error: "); fprintf(stderr, "%d", errno); fprintf(stderr, "\n"); Even so at the filesystem level, the i/o from write() is allowed to be interleaved though in practice you will almost never see that. -- Joe Seigh When you get lemons, you make lemonade. When you get hardware, you make software. .