[Swan] No problem with syslog in a threaded context on Fedora 18

Philippe Vouters philippe.vouters at laposte.net
Thu Mar 7 07:40:35 EET 2013


Dear all,

If I execute this code on my dual core processor running Fedora 18, I 
incur strictly no hang. Would this mean the syslog call is actually 
thread-safe ?

$ cat test_syslog_threaded.c
#include <stdio.h>
#include <syslog.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>

#define NUMBER_OF_THREADS 4

static int gate;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void do_syslog_entries(void){
     char *host="foo";

     syslog (LOG_MAKEPRI(LOG_LOCAL1, LOG_ERR),
             "Getting connected to %s.",host);
     errno=ECONNABORTED;
     syslog (LOG_ERR,
             "Unable to make network connection to %s.  Error=%m", host);
}

void *thread(void *p) {
     int ret = 0;

     pthread_mutex_lock(&mutex);
     while (!gate && !ret)
            ret = pthread_cond_wait(&cond,&mutex);
     pthread_mutex_unlock(&mutex);
     do_syslog_entries();
     return NULL;
}

int main(int argc, char **argv) {
     pthread_t thread_id[NUMBER_OF_THREADS];
     void *ThreadExitStatus;
     int i;

     for (i=0; i < NUMBER_OF_THREADS; i++)
          pthread_create(&thread_id[i], NULL, thread, NULL);
     usleep(100);
     gate = 1;
     pthread_cond_broadcast(&cond);
     for (i=0; i < NUMBER_OF_THREADS; i++)
          pthread_join(thread_id[i], &ThreadExitStatus);
     printf("Program exiting...\n");
     return 0;
}
$

-- 
Philippe Vouters (Fontainebleau/France)
URL: http://vouters.dyndns.org/
SIP: sip:Vouters at sip.linphone.org



More information about the Swan mailing list