Thread: fprintf functionality in a multithreaded application

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Colombo, Sri Lanka
    Posts
    5

    Question fprintf functionality in a multithreaded application

    I'm writing a server which has multiple threads serving different requests. Within each thread, log entries should be written to a single common log file. I'm unsure about the fprintf's functionality in this context.

    Does the fprintf(log_file, ,) of other threads block until current thread is done with writing to the log_file (actually to the output buffer)?

    I'm asking this to understand whether it is necessary to lock that code section. If fprintf blocks, then locking is not necessary, isn't it?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    That depends on your C library. I think for glibc if you link with pthread lib, the function is thread safe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do you mean all threads should log to the same file? As Bayint mentioned, fprintf is thread safe, i.e. there are no static local variables that are shared across threads, that could cause problems with mixed data.

    If you're asking about preventing one thread from writing to the log file until the other is done, then no, fprintf doesn't lock a file like that. You can happily have several threads each writing to the same file, and their output will be interleaved. The threads execute in an unspecified order, and you don't know if the output buffer of one will be flushed before switching to the next thread, which may write it's output and flush the buffer to file.

    You can protect this with a mutex however, so that each thread is guaranteed to write it's full log message to the file without interleaving. Here's a test program that will show the difference between using a mutex and not using one. It uses stdout, but the effects are the same as writing to a file.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <time.h>
    #include <pthread.h>
    
    #define NUM_THREADS     5
    
    static int do_mutex;
    static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
    
    static void *print_hello(void *thread_id)
    {
        int i;
        long tid = (long) thread_id;
    
        if (do_mutex) {
            pthread_mutex_lock(&mut);
        }
    
        for (i = 0; i < 3; i++) {
            usleep(rand() % 100000);        // kill a little time
            printf("Thread %ld, loop %d\n", tid, i);
        }
    
        if (do_mutex) {
            pthread_mutex_unlock(&mut);
        }
    
        pthread_exit(NULL);
    }
    
    int main(int argc, char *argv[])
    {
        int rc;
        long t;
        pthread_t threads[NUM_THREADS];
    
        if (argc > 2) {
            fprintf(stderr, "Usage: threads [mutex]\n");
            fprintf(stderr, "\twhere 'mutex' causes the thread to lock "
                    "until all 3 loops are done\n");
            exit(1);
        }
    
        do_mutex = (argc == 2) && !strcmp(argv[1], "mutex");
    
        srand(time(NULL));
    
        for (t = 0; t < NUM_THREADS; t++) {
            rc = pthread_create(&threads[t], NULL, print_hello, (void *) t);
            if (rc) {
                perror("pthread_create");
                exit(1);
            }
        }
        pthread_exit(NULL);
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    Colombo, Sri Lanka
    Posts
    5
    Thanks a lot for your replies Bayint and anduril462.

    In my application all the threads write to the same file and they must do it simultaneously. Only condition needs to be satisfied is that each fprintf must write its message completely.

    For example. If thread t1 is using...

    fprintf(log_file, "Hello world");

    and thread t2 is using...

    fprintf(log_file, "Hi");


    The output should not be something like...

    Hello Hi world

    I already link my app with the pthread library. And anduril462, according to the output of your program, it seems that each fprintf does its job completely before the thread gets preempted, even when no mutexes are used. Is this behavior ensured in every situation?

  5. #5
    Registered User
    Join Date
    Mar 2011
    Location
    Colombo, Sri Lanka
    Posts
    5
    Sorry I missed another question that is relevant to the previous post.

    Quote Originally Posted by Bayint Naung View Post
    That depends on your C library. I think for glibc if you link with pthread lib, the function is thread safe.
    Currently I give -pthread flag at the compile time and -lpthread at the linking time. Is giving this at two places necessary? What is the correct way? Thanks

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Colombo, Sri Lanka
    Posts
    5
    Any help?

    I just want to know whether flushing an output buffer (automatically or by force) causes everything inside it to be flushed completely? or is there a possibility of that thread getting exempted before everything is flushed?

    Thanks..

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you really want to be sure, put the fprintf along with an fflush inside a mutex lock. that will ensure everything the thread has to write gets written. The cost is minimal for the peace of mind you get.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Location
    Colombo, Sri Lanka
    Posts
    5
    Quote Originally Posted by anduril462 View Post
    If you really want to be sure, put the fprintf along with an fflush inside a mutex lock. that will ensure everything the thread has to write gets written. The cost is minimal for the peace of mind you get.
    Thanks anduril462.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket programming in C on windows 7
    By wahla in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-27-2010, 08:59 AM
  2. Replies: 0
    Last Post: 11-22-2009, 11:23 AM
  3. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  4. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM