logging-log4cxx-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p...@mu.org
Subject Race condition with FileAppender.
Date Fri, 29 Jun 2007 13:09:26 GMT

Hi, I've been using the subversion log4cxx-10.0.0, which is currently at revision 551872.

I'm running on an ubuntu linux box running kernel 2.6.20-16, and g++ 4.1.2

I've been seeing severe race conditions between threads when logging to a FileAppender.

Basically, when multiple threads write to the same log file, some of the data is corrupt.

Here is a little program will show the issue.

I've been pouring over the docs, and the mailing list archives, but I can't find a clear explanation
as to why this shouldn't work.

Any ideas what I'm doing wrong?  Thanks, Pete

----------------------------------------------------------------------------
#include <pthread.h>

#include <iostream>
#include <sstream>
#include <vector>

#include <log4cxx/logger.h>

const int NUMTHREADS = 100 ;
const int NUMWRITES = 100 ;

log4cxx::LoggerPtr thread_logger = log4cxx::Logger::getLogger("ThreadManager");
using std::cerr;
using std::endl;

void *Run( void *ptr ){

    int *thread_number = reinterpret_cast<int*>(ptr);

    for (int i=0 ; i< NUMWRITES ; i++){
        std::ostringstream msg;
        msg << "Thread[" << *thread_number << "], Write [" << ( i
+ 1 ) << "]";
        LOG4CXX_DEBUG(thread_logger, msg.str());
    }

    pthread_exit(NULL);

}

int 
main(int argc, char *argv[]) {

    std::vector<pthread_t> threads(NUMTHREADS);

    log4cxx::LoggerPtr rootlogger = log4cxx::Logger::getRootLogger();

    int         thread_number = 0; 
    int         *thread_cnt = 0;
    while (thread_number++ < NUMTHREADS){

        thread_cnt =  new int(thread_number);
        pthread_create(&threads[thread_number -1], NULL, Run, (void *) thread_cnt);
    }
    
    thread_number = NUMTHREADS;

    while (--thread_number >= 0){
        pthread_join(threads[thread_number], NULL);
    }

    return 0;

}
----------------------------------------------------------------------------

Here is my log4cxx.properties:

log4j.debug=true

# Loggers
log4j.rootLogger=DEBUG, rootlog
log4j.logger.ThreadManager=DEBUG, ThreadManager

# Additivity
log4j.additivity.ThreadManager=false

# rootLogger
log4j.appender.rootlog.File=root.log
log4j.appender.rootlog.Append=false
log4j.appender.rootlog=org.apache.log4j.FileAppender
log4j.appender.rootlog.layout=org.apache.log4j.PatternLayout 
log4j.appender.rootlog.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# ThreadManager
log4j.appender.ThreadManager=org.apache.log4j.FileAppender 
log4j.appender.ThreadManager.File=ThreadManager.log
log4j.appender.ThreadManager.Append=false
log4j.appender.ThreadManager.layout=org.apache.log4j.PatternLayout 
log4j.appender.ThreadManager.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n



Mime
View raw message