https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126005
Bug ID: 126005
Summary: ThreadSanitizer: data race in
std::print/std::println/std::vprint_nonunicode
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: bugzilla.krzysdz at gmail dot com
Target Milestone: ---
TSan reports data races when using std::println, std::print or
std::vprint_nonunicode to output to stdout (default for print and println).
~~I cannot reproduce this on my computer, but~~ I found this while testing
simple multithreaded code on Compiler Explorer -
https://godbolt.org/z/G4afzYb1v. On Compiler Explorer I have tested and
successfully reproduced it on x86-64 16.1 and trunk.
I could not reproduce it, because I used 15.2.0 (more on this later).
Using std::cout instead of stdout fixes this problem.
The reported data race is r/w access to _M_file->_IO_write_ptr by
__format::_File_sink::_File.
Simplest code that ThreadSanitizer complains about (-std=c++23
-fsanitize=thread):
===================================================
#include <thread>
#include <print>
void f() { std::println("Hello world!"); }
int main() {
auto t = {std::jthread(&f), std::jthread(&f)};
}
===================================================
The bug can only be reproduced with 16.1.0 and later versions. The problem was
introduced by bug 121790.