[Bug libstdc++/89740] New: reading from cin & writing to cout sync_with_stdio(false) has race conditions

ahu at ds9a dot nl gcc-bugzilla@gcc.gnu.org
Sat Mar 16 22:39:00 GMT 2019


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

            Bug ID: 89740
           Summary: reading from cin & writing to cout
                    sync_with_stdio(false) has race conditions
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ahu at ds9a dot nl
  Target Milestone: ---

Created attachment 45981
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45981&action=edit
a small reproduction for this bug.

Reading lots of lines from cin in main() and writing to cout from an additional
thread usually works fine. However, when doing sync_with_stdio(false),
duplicate output appears on the terminal. No C stdio is performed by the
program. No two threads are attempting simultaneous output. 

Output of attached 25 line reproduction:

$ yes | ./repro
HHi 0
Hi Hi 1
Hi Hi 2

Verification with strace shows that the main thread is doing writes to file
descriptor 1 that are also happening in the output thread.

The problem goes away when setting 'cin.tie(nullptr)' or when removing
sync_with_stdio(false):
$ yes | ./repro
Hi 0
Hi 1
Hi 2

A trivial reproduction is attached.

The hypothesis is that cin/cout tying is somehow relying on sync_with_stdio to
prevent race conditions.

Bug #70276 touches on slightly similar material, but it looks like something
different.


More information about the Gcc-bugs mailing list