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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:44:25+00:00 2026-05-31T20:44:25+00:00

From what I read, the standard output streams are generally not thread safe .

  • 0

From what I read, the standard output streams are generally not thread safe. I have a C++ application (Windows-based, using Visual Studio 2005) that has a very simple logging function:

void logText(string text)
{
    if(g_OutputLogEnabled && g_OutputLog.is_open())
    {
        string logDate = getDateStamp("%Y-%m-%d %H:%M:%S");
        g_OutputLog << "[" << logDate << "]: " << text << endl;
    }

    cout << text << endl; // Also echo on stdout
}

In this example, g_OutputLog is an ofstream and g_OutputLogEnabled is a boolean.

I’ve been using this small function in my main application with no problem, but I now want to extend its use to some child threads. These threads do work and asynchronously print data as that work is done.

Question: How can I add simple, line-level thread-safety to this routine? All that I really care about is that each line that comes in remains intact in my log. Performance isn’t a concern in this case, but (as always) faster is nicer.

I’m aware I could use third party logging packages, but I want to do it myself so I can learn how it works. My multi-threading knowledge isn’t what it should be, and I’m trying to improve that.

I’ve heard the term critical sections, and I’m somewhat aware of mutexes and semaphores, but which would I use in this case? Is there a clean, simple solution? Thanks in advance for any advice.

  • 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-31T20:44:27+00:00Added an answer on May 31, 2026 at 8:44 pm

    Use scoped lock such as:

    void logText(string text)
    {
        if(g_OutputLogEnabled && g_OutputLog.is_open())
        {
            string logDate = getDateStamp("%Y-%m-%d %H:%M:%S");
    
            boost::scoped_lock (g_log_mutex);  //lock
            g_OutputLog << "[" << logDate << "]: " << text << endl;
    
        } //mutex is released automatically here
        
        boost::scoped_lock (g_cout_log_mutex); //lock on different mutex!
        cout << text << endl; // Also echo on stdout
    }
    

    Or you could use std::unique_lock if your compiler supports this.


    How would you implement scoped_lock if you cannot use Boost and if you don’t have std::unique_lock?

    First define mutex class:

    #include <Windows.h>
    
    class mutex : private CRITICAL_SECTION  //inherit privately!
    {
    public:
         mutex() 
         {
            ::InitializeCriticalSection(this);
         }
         ~mutex() 
         {
            ::DeleteCriticalSection(this);
         }
    private:
    
         friend class scoped_lock;  //make scoped_lock a friend of mutex
    
         //disable copy-semantic 
         mutex(mutex const &);           //do not define it!
         void operator=(mutex const &);  //do not define it!
    
         void lock() 
         {
            ::EnterCriticalSection(this);
         }
         void unlock() 
         {
            ::LeaveCriticalSection(this);
         }
    };
    

    Then define scoped_lock as:

    class scoped_lock
    {
          mutex & m_mutex;
      public:
          scoped_lock(mutex & m) : m_mutex(m) 
          {
              m_mutex.lock();
          }
          ~scoped_lock()
          {
              m_mutex.unlock();
          }
    };
    

    Now you can use them.

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

Sidebar

Related Questions

I'm reading from the standard input using the read() system call but there's a
Let's say I want to read a line from a socket, using the standard
I have read from some article that say's Apple doesn't approve the application which
I want to be able to write bytes and read them from standard input/output
I am using standard input and output to pass 2 base64 strings from one
I'm looking to parse UTF8 characters from the standard output stream of another application
Is there any way to read standard output/error from any, GUI and non-GUI, already
How can I read from standard input and write to standard output. System.Diagnostics.Process.StandardInput's MSDN
I read that SHA-1 is being retired from the FIPS 180-2 standard. Apparently there
What's ActivePython actually about? From what I've read it's just standard Python with OpenSSL

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.