Thread: Dumb I/O Performance Question

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Visual Studio provides several versions of standard CRT library, Multi-threaded versions have internal locking in the IO functions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Registered User
    Join Date
    Feb 2008
    Posts
    6
    Quote Originally Posted by BENCHMARKMAN View Post
    Efficencly is actually a secondary effect of what I am trying to accomplish. I'm creating 20 thread that print debut and using multiple printf statments causes the output to be interleaved.
    If you must use the same output stream, and you want to stop interleaving, you can serialize them via a global mutex and use fflush():

    Code:
            /* body of individual thread */
            pthread_mutex_lock(&output_mutex);
            fprintf(stdout, "individual thread output 1\n");
            fprintf(stdout, "individual thread output 2\n");
            fprintf(stdout, "individual thread output 3\n");
            fprintf(stdout, "individual thread output 4\n");
            fflush(stdout);
            pthread_mutex_unlock(&output_mutex);
    I wouldn't even waste time looking for this to be a significant bottleneck either, unless you're pumping out lots of output - and if you are, you're hitting other locks anyways.

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    better yet - implement some queue and loggerThread

    All threads post their logs to queue (using appropriate locking)
    One thread - does nothing except reading from the queue and printing messages
    This will prevent blocking working threads due to IO delays
    On the other hand loggerThread will be mostly waiting for IO operation complition without using to much CPU
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Thanks for the replies. I didn't really want to use a mutex becaues the printf() is already in a locked section.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very dumb question.
    By Blips in forum C++ Programming
    Replies: 14
    Last Post: 11-08-2005, 09:37 AM
  2. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 03:27 PM
  3. C++ File I/O question
    By zero_cool in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2005, 10:43 AM
  4. File I/O Question
    By Sentral in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2005, 04:49 AM
  5. dumb question...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-28-2002, 10:35 AM