<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Dec 7, 2016 at 10:02 AM, Mehdi Amini via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
> On Dec 7, 2016, at 1:52 AM, Viacheslav Nikolaev via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> This code from raw_ostream.h is really racy:<br>
><br>
> raw_ostream &operator<<(StringRef Str) {<br>
> // Inline fast path, particularly for strings with a known length.<br>
> size_t Size = Str.size();<br>
><br>
> // Make sure we can use the fast path.<br>
> if (Size > (size_t)(OutBufEnd - OutBufCur))<br>
> return write(Str.data(), Size);<br>
><br>
> if (Size) {<br>
> memcpy(OutBufCur, Str.data(), Size);<br>
> OutBufCur += Size;<br>
> }<br>
> return *this;<br>
> }<br>
<br>
</span>I don’t believe "the is racy” is an appropriate qualification, “buffered raw_ostream are not providing a thread-safe API" seems more accurate to me.</blockquote><div><br></div><div>I agree. Acquiring a lock on each write to a buffered raw_ostream is too expensive. You can always explicitly use std::mutex if you have shared raw_ostreams.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
<br>
> Of course, one might wonder why someone would need to output to a stream from multiple threads at the same time.<br>
><br>
> But imagine someone might get logs to dbgs() or errs() running the backend for a target in multiple threads.<br>
<br>
</span>These are unbuffered, I wouldn’t expect a race in the code you list above. I believe it’ll always forward directly to raw_fd_ostream::write_impl(), which is calling the libc ::write().<br>
<br>
—<br>
Mehdi<br>
<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br></div></div>