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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:32:28+00:00 2026-05-22T19:32:28+00:00

I have a log class, that class contains a stream defined as: ofstream logfile

  • 0

I have a log class, that class contains a stream defined as:ofstream logfile and a mutex to make sure that each time only one thread writes to the file (the program is multi-threaded).
The class is defined as:

#define LOG_NAME "log.txt"

using namespace std;

class Log
{
private:
    pthread_mutex_t mutex_write;
    ofstream logfile;

public:
    Log();
    ~Log();
    void Write (string txt);
};

The constructor is:

Log::Log()
{
    pthread_mutex_init (&mutex_write,NULL);
    pthread_mutex_lock (&mutex_write);
    logfile.open(LOG_NAME, ios::out | ios::trunc);
    logfile << "Created log file named " << LOG_NAME << endl;
    pthread_mutex_unlock (&mutex_write);
}

The destructor is:

Log::~Log()
{
    logfile << "Closing log file" << endl;
    pthread_mutex_lock (&mutex_write);
    logfile.close();
    pthread_mutex_unlock (&mutex_write);
    pthread_mutex_destroy (&mutex_write);
}

and:

void Log::Write (string txt)
{
    pthread_mutex_lock (&mutex_write);
    logfile << txt << endl;
    pthread_mutex_unlock (&mutex_write);
}

In some of the times when the destructor is called, it can’t execute the line logfile.close(); because it says that it gets a segmentation fault, or it displays the message:

*** glibc detected *** corrupted double-linked list: 0x0000000000513eb0 ***
Abort

This doesn’t happen all the time, it seems to be happening randomly, at about 10% of the time. The program is multi-threaded (under linux).

Edit:
example of usage: (where log is a pointer to an object of Log class)

stringstream str;
str.str("");
str << "Ant " << i << " was created at place: (" << x << "," << y << ")";
log->Write (str.str());

or, if the string contains only knows text

log->Write ("Created board entity");
  • 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-22T19:32:29+00:00Added an answer on May 22, 2026 at 7:32 pm

    The problem was that we have a problem with checking that all threads have finished. We have repaired this, and now it works fine.
    The problem probably happened because other threads tried to access a closed file, and sometimes tried to access a killed entity.

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

Sidebar

Related Questions

I have a log file in SqlServer that stores the time an application started,
I have a web app that contains an activity log for actions the users
I have a WCF solution that consists of the following class libraries: Exercise.Services: Contains
I have a class that encapsulates tcp socket communications with a server. For each
I have a static class which only contains string properties. I want to convert
I have a submenu that contains 3 choices for the user. Each option when
I am designing a class for log entries of my mail server. I have
We've faced strange problem. We have log on service, that authenticates user, adds auth
i have a log file which contains hundreds/thousands of seperate XML messages and need
I have a log that is really huge. (millions of rows) LogTable ------- 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.