Visual Studio provides several versions of standard CRT library, Multi-threaded versions have internal locking in the IO functions
Printable View
Visual Studio provides several versions of standard CRT library, Multi-threaded versions have internal locking in the IO functions
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():
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.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);
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
Thanks for the replies. I didn't really want to use a mutex becaues the printf() is already in a locked section.