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

  • Home
  • SEARCH
  • 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 6169831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:55:29+00:00 2026-05-23T22:55:29+00:00

I am working on a multithreaded logging for my engine. But I ma having

  • 0

I am working on a multithreaded logging for my engine. But I ma having troubles making it MT. The problem is, even if I am deep copying my logger object into a local variable I am going to have problems with the files. Because two threads are going to write to the same file at the same time which will build a mess. Here is my logger class:

class Logger {
public:
    typedef std::vector<LogListener *> ListenerList;

private:
    ListenerList listeners;
            boost::mutex mutex;

public:
    Logger();
    ~Logger();

    Logger * write(const String &line);

};

Logger * Logger::write(const String &text) {

    if(listeners.empty()) return this;
    boost::unique_lock<boost::mutex> lock(mutex);
    for(ListenerList::iterator i = listeners.begin(); i != listeners.end(); ++i) {
        (*i)->write(text);
    }

    return this;

}

class FileLogListener : public LogListener {
    std::ofstream stream;
public:
    FileLogListener(const String &str) : stream(str.c_str(), std::ios::out | std::ios::app) { }
    void write(const String &text) {
        stream << text << std::endl;
    }

};

Now suppose (avoiding mutexes):

//Thread 1

void func1() {
    Logger * log = new Logger;
    log->addListener("file.txt");
    log->write("Thread 1 Test");
}

//Thread 2

void func2() {
    Logger * log = new Logger;
    log->addListener("file.txt");
    log->write("Thread 2 test");
}

int main() {
  boost::thread t1(&func1);
  boost::thread t2(&func2);

  t1.join();
  t2.join();
  return 0;
}

“file.txt” becomes a mess.

EDIT: Currently, I am reading and watching lectures on Multithreading to understand it better.

EDIT: The above logger works.

Thanks in advance,
Gasim Gasimzada

  • 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-23T22:55:29+00:00Added an answer on May 23, 2026 at 10:55 pm

    once I’ve written this article about minimalist logger. it’s a single header file simple logger that can be used in MT environment. it satisfied my logging needs in lots of projects

    If you want to use your logger, you can just check the approach

    in short: it just locks access to the file by boost::mutex

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

Sidebar

Related Questions

I'm working on a multithreaded server in c++ using boost-asio. Currently a design problem
I'm working on a multithreaded C++ application that is corrupting the heap. The usual
I'm working on a multithreaded application, and I want to debug it using GDB.
I have very little experience working with sockets and multithreaded programming so to learn
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch
I'm working on a multithreaded (pthread based) project. The project uses a library that
I am working on a multithreaded program using C++ and Boost. I am using
I have a project that I have recently started working on seriously but had
I have a fairly complex multi-threaded Windows service working, but I can't figure out
I am working on a multithreaded application (C#), and 2 threads are updating a

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.