Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6048319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:26:59+00:00 2026-05-23T07:26:59+00:00

In general I assume that streams are not synchronized, it is up to the

  • 0

In general I assume that streams are not synchronized, it is up to the user to do appropriate locking. However, do things like cout get special treatment in the standard library?

That is, if multiple threads are writing to cout can they corrupt the cout object? I understand that even if synchronized you’d still get randomly interleaved output, but is that interleaving guaranteed. That is, is it safe to use cout from multiple threads?

Is this vendor dependent? What does gcc do?


Important: Please provide some kind of reference for your answer if you say “yes” since I need some kind of proof of this.

My concern is also not about the underlying system calls, those are fine, but the streams add a layer of buffering on top.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:27:00+00:00Added an answer on May 23, 2026 at 7:27 am

    The C++03 standard does not say anything about it. When you have no guarantees about the thread-safety of something, you should treat it as not thread-safe.

    Of particular interest here is the fact that cout is buffered. Even if the calls to write (or whatever it is that accomplishes that effect in that particular implementation) are guaranteed to be mutually exclusive, the buffer might be shared by the different threads. This will quickly lead to corruption of the internal state of the stream.

    And even if access to the buffer is guaranteed to be thread-safe, what do you think will happen in this code?

    // in one thread
    cout << "The operation took " << result << " seconds.";
    
    // in another thread
    cout << "Hello world! Hello " << name << "!";
    

    You probably want each line here to act in mutual exclusion. But how can an implementation guarantee that?

    In C++11, we do have some guarantees. The FDIS says the following in §27.4.1 [iostream.objects.overview]:

    Concurrent access to a synchronized (§27.5.3.4) standard iostream object’s formatted and unformatted input (§27.7.2.1) and output (§27.7.3.1) functions or a standard C stream by multiple threads shall not result
    in a data race (§1.10). [ Note: Users must still synchronize concurrent use of these objects and streams by
    multiple threads if they wish to avoid interleaved characters. — end note ]

    So, you won’t get corrupted streams, but you still need to synchronize them manually if you don’t want the output to be garbage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

general question is i like to build logger class that writes to single log
Please assume that I do not need to worry about development time and costs:
This is somewhat of a general question, is it realistic to assume that a
I'm using OpenLayers to display OpenStreetMap maps. (Though, I'd assume this should be general
It is obvious that in general the read(2) system call can return less bytes
Assume that I want to draw samples from some probability distribution. In the case
In general, the == operator is not suited to test for numeric equality, but
I created a special-purpose programming language that deliberately (by design) cannot evaluate the same
I try to understand how axiom resolution works in prolog. Let's assume that I
This is a general DB design question. Assume the following table: ====================================================================== | product_translation_id

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.