Threadsafe alternatives to log4c that support logging in microseconds

Posted on 2010-09-16
Last Modified: 2012-05-10
Hi guys,

Again, as per title. I need a logging solution that's thread safe and supports logging in microseconds.....
Question by:PMembrey
  • 4
LVL 32

Expert Comment

ID: 33698129
From previous question, you already have a function that can do the logging with microseconds. To make this function thread-safe, you can add take a mutex at the entry point of the function, and release the mutex at the exit point.

Below is code that shows how to use a mutex to protect your function. Code is taken from

// ************* short example *******************

/* Note scope of variable and mutex are the same */

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int counter=0;

/* Function C */

void functionC()


   pthread_mutex_lock( &mutex1 );


   pthread_mutex_unlock( &mutex1 );


// ******************** A sample Program ***********************

#include <stdio.h>

#include <stdlib.h>

#include <pthread.h>

void *functionC();

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

int  counter = 0;



   int rc1, rc2;

   pthread_t thread1, thread2;

   /* Create independent threads each of which will execute functionC */

   if( (rc1=pthread_create( &thread1, NULL, &functionC, NULL)) )


      printf("Thread creation failed: %d\n", rc1);


   if( (rc2=pthread_create( &thread2, NULL, &functionC, NULL)) )


      printf("Thread creation failed: %d\n", rc2);


   /* Wait till threads are complete before main continues. Unless we  */

   /* wait we run the risk of executing an exit which will terminate   */

   /* the process and all threads before the threads have completed.   */

   pthread_join( thread1, NULL);

   pthread_join( thread2, NULL); 



void *functionC()


   pthread_mutex_lock( &mutex1 );


   printf("Counter value: %d\n",counter);

   pthread_mutex_unlock( &mutex1 );


Open in new window


Author Comment

ID: 33698139
this is what I have now, I was just hoping that there might be a more featureful solution already available.
LVL 32

Accepted Solution

phoffric earned 500 total points
ID: 33698149
Couldn't you wrap the useful features of log4c with a mutex (just be careful that one log4c function doesn't call another that is wrapped with the same mutex). Or to do it without analysis, you could have a distinct mutex per log4c function. And then you just call your log4c_ts functions.
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

LVL 32

Expert Comment

ID: 33698190
I don't know how complex or the performance requirements of your system is. But in one highly real-time system, having each task do its own logging resulted in unacceptable degradation of the system. So, instead, the architects just added a logging task which accepted messages in its queue. With this approach, there were naturally no thread-safe issues. This logging task did the time-stamping so that the real-time tasks didn't need the extra overhead. The time-stamping results were sufficient for our needs even though they may be a ms off.
LVL 32

Expert Comment

ID: 33698211
FYI - I came across this link on log4cplus which says the package is thread-safe.
LVL 53

Expert Comment

ID: 33698787
In your other question (, it was mentioned how to use log4c in a thread safe way.

I'm curious though : are you sure you really need microsecond precision logs ? That's rarely useful, especially because the logging operation itself will likely take much more than a few microseconds.

I mentioned it in one of your other questions too : if you really need a micro-second-precision timer (from the start of the executable generally) mentioned in the logs, you can always add it. Nothing prevents you from doing that.
A micro-second precision timestamp (with date and time of day) though is a very different matter. Very few systems support something like that.

So, you might want to re-consider this ;)

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now