# C++ iostreams: Unexpected but legal multithreaded behaviour

DevFeed: [C++ iostreams: Unexpected but legal multithreaded behaviour](<https://devfeed.tech/articles/c-iostreams-unexpected-but-legal-multithreaded-behaviour-36453.md>)

Original publisher: [Read original article](<https://berthub.eu/articles/posts/iostreams-unexpected/>)

Published: 2019-03-17T16:46:37Z

Content type: tutorial

Language: en

Sources: [Bert Hubert's writings](<https://devfeed.tech/sources/bert-hubert-s-writings.md>)

Topics: [C++](<https://devfeed.tech/topics/c-plus-plus.md>), [IO](<https://devfeed.tech/topics/io.md>), [Streams](<https://devfeed.tech/topics/streams.md>), [Programming](<https://devfeed.tech/topics/programming.md>), [bug](<https://devfeed.tech/topics/bug.md>)

Tags: [bug](<https://devfeed.tech/tags/bug.md>), [c](<https://devfeed.tech/tags/c.md>), [c-plus-plus](<https://devfeed.tech/tags/c-plus-plus.md>), [issue](<https://devfeed.tech/tags/issue.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [programming](<https://devfeed.tech/tags/programming.md>)

## AI overview

This article examines a C++ iostreams gotcha that can cause duplicate or corrupted output in multithreaded programs after optimization is enabled. It explains the relationship between C stdio and C++ iostream buffering and discusses the performance and usability trade-offs of iostreams.

## Source excerpt

In previous articles, I've waxed rhapsodic about how great C++ is. I also noted there however that every language, C++ included, has its dark sides. Some languages have an unavoidable pervasive dark side, like being slow or hard to multithread, for C++ that dark side is mostly its complexity. In this post I want to zoom in on a specific 'gotcha' that recently took me several hours to resolve. I wrote this piece so anyone running into the same issue might find out about it if they search the web.