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 9251549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:43:45+00:00 2026-06-18T10:43:45+00:00

I have a multi-threaded application, which heavily uses std::cout for logging without any locking.

  • 0

I have a multi-threaded application, which heavily uses std::cout for logging without any locking. In such a case, how can I easily add lock mechanism to make std::cout thread-safe?

I don’t want to search for each occurrence of std::cout and add a line of locking code. That is too tedious.

Any better practice?

  • 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-06-18T10:43:47+00:00Added an answer on June 18, 2026 at 10:43 am

    Note: This answer is pre-C++20 so it does not use std::osyncstream with its separate buffering, but uses a lock instead.

    I guess you could implement your own class which wraps cout and associates a mutex with it. The operator << of that new class would do three things:

    1. create a lock for the mutex, possibly blocking other threads
    2. do the output, i.e. do the operator << for the wrapped stream and the passed argument
    3. construct an instance of a different class, passing the lock to that

    This different class would keep the lock and delegate operator << to the wrapped stream. The destructor of that second class would eventually destroy the lock and release the mutex.

    So any output you write as a single statement, i.e. as a single sequence of << invocations, will be printed atomically as long as all your output goes through that object with the same mutex.

    Let’s call the two classes synchronized_ostream and locked_ostream. If sync_cout is an instance of synchronized_ostream which wraps std::cout, then the sequence

    sync_cout << "Hello, " << name << "!" << std::endl;
    

    would result in the following actions:

    1. synchronized_ostream::operator<< would aquire the lock
    2. synchronized_ostream::operator<< would delegate the printing of “Hello, ” to cout
    3. operator<<(std::ostream&, const char*) would print “Hello, “
    4. synchronized_ostream::operator<< would construct a locked_ostream and pass the lock to that
    5. locked_ostream::operator<< would delegate the printing of name to cout
    6. operator<<(std::ostream&, std::string) would print the name
    7. The same delegation to cout happens for the exclamation point and the endline manipulator
    8. The locked_ostream temporary gets destructed, the lock is released
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi threaded client server application which uses Sockets. When a new
I have an existing multi-threaded application which uses blocking connect() call. However, I want
I have a multi-threaded application in C# which tries to write to a TextBox
For example, I have multi-threaded application which can be presented as: Data bigData; void
I have a multi-threaded application which parses some text and it needs to use
I'm trying to solve a classic problem - I have a multi-threaded application which
Here's the scenario: I have a multi threaded java web application which is running
I have a multi-threaded application which needs to display certain dates to the user.
I've to move a Windows based multi-threaded application (which uses global variables as well
We have created a multi-threaded application which process/parse big files (few hundred MB's) simultaneously.

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.