[Bug libstdc++/70276] New: Writing to standard output concurrently through `std::cout` triggers a datarace

stefan at vectorfabrics dot com gcc-bugzilla@gcc.gnu.org
Thu Mar 17 13:40:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70276

            Bug ID: 70276
           Summary: Writing to standard output concurrently through
                    `std::cout` triggers a datarace
           Product: gcc
           Version: 4.9.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stefan at vectorfabrics dot com
  Target Milestone: ---

Consider the following C++11 program:

  #include <cstdlib> 
  #include <future> 
  #include <iostream> 

  int main() 
  { 
    std::future<void> future = 
      std::async(std::launch::async, []() { std::cout << "foo" << std::endl;
}); 
    std::cout << "bar" << std::endl; 
    future.wait(); 

    return EXIT_SUCCESS; 
  }

This program exhibits a datarace for the member variable
`std::ios_base::_M_width` of `std::cout`.

For example, Pareon Verify reports (some details elided for clarity):

  [M0108] Data race(s) detected:
    an object of size 272
    is concurrently accessed by
    the write in
      function ios_base::width at .../include/bits/ios_base.h:656
      called from function std::__ostream_insert at
.../include/bits/ostream_insert.h:102
      called from function std::operator<< at .../include/ostream:535
      called from function main at test.cpp:9
      ^^^ application start ^^^
    performing 1 access of size 8 at an offset of 24 bytes from the start of
the object
    and the read in
      function ios_base::width at .../include/bits/ios_base.h:645
      called from function std::__ostream_insert at
.../include/bits/ostream_insert.h:87
      called from function std::operator<< at .../include/ostream:535
      ...
      called from function execute_native_thread_routine at .../thread.cc:84
      ^^^ thread start ^^^
      called from function pthread_create
      ...
      called from function std::async at include/c++/4.9.2/future:1580
      called from function main at test.cpp:8
      ^^^ application start ^^^
    performing 1 access of size 8 at an offset of 24 bytes from the start of
the object

Helgrind produces a similar (albeit somewhat more cryptic) message.


More information about the Gcc-bugs mailing list