Is fprintf thread safe for OS X?

Is fprintf thread safe for OS X? If so, where is this documented?

+7
thread-safety printf macos
source share
2 answers

This was a good question, although similar questions have been asked here many times. I was interested in the OSX aspect, because I myself tried to get up to speed on this system. (maybe you should add the OSX tag)

I THINK fprintf () is thread safe on OSX. My first reason is that Darwin’s people went in that direction, as evidenced by their choice to abandon the global “errno” old school in favor of the errno () function. For documentation just follow "/usr/include/errno.h". Without this, none of the libc material would be thread safe. However, using errno () does not say anything about fprintf (). This is just the beginning. I’m sure everyone knows at least one situation in which Apple hasn’t done a good idea.

Another reason I believe in the "thread safety" of fprintf () is the source code , which should be "real" until at least 10.6 when Apple closed OSX (part / all). Scan this code for "MT-Safe" and you will see CLAIM that the non-local version of 'vfprintf ()' is thread safe. Again, this does not prove anything. However, this is the form of documentation you wanted.

My last reason to believe fprintf () is thread safe is a test case. This also proves nothing. Perhaps this proves that the buffer space is thread safe. Well, that was an occasion to write a little program for fun. In fact, I did not write this. I found the skeleton online and changed it. The definition of "FLUSH_BUFFER" allows you to see more clearly what is happening. If this macro is not defined, you get a "sort-of" buffer test (the same text without any linear terminators). I could not figure out how to organize a more meaningful collision of threads.

I assume you can write multiple files. Writing to a single file is probably the best test. The included program is not a final test. Although this can be expanded, I'm not sure that any program can really be final. Bottom line: maybe you should just MUTEX your calls to fprintf ().

// artificial test for thread safety of fprintf() // define FLUSH_BUFFER to get a good picture of what happening, un-def for a buffer test // the 'pretty print' (FLUSH_BUFFER) output relies on a mono-spaced font // a writeable file name on the command line will send output to that file // #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define FLUSH_BUFFER #define NTHREAD 5 #define ITERATIONS 3 const char DOTS[] = ". . . . . . . . . . . . . . . . . . . . . . . . . . . . "; FILE *outFile; void *PrintHello(void *threadid) { long tid; tid = (long)threadid; for (int i=1; i<=ITERATIONS; i++) { long delay = (NTHREAD-tid) * 100000 + (ITERATIONS-i+1) * 10000; #ifdef FLUSH_BUFFER fprintf(outFile, "%*sStart thread %d iteration %d\n", (tid+1)*4, " ", tid, i); usleep(delay); fprintf(outFile, "%*sFinish thread %d iteration %d %*.*sw/delay %d\n", (tid+1)*4, " ", tid, i, (NTHREAD-tid+1)*4, (NTHREAD-tid+1)*4, DOTS, delay); #else fprintf(outFile, "Start thread %d iteration %d ", tid, i); usleep(delay); fprintf(outFile, "Finish thread %d iteration %dw/delay %d\n", tid, i, delay); #endif } pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NTHREAD]; char errStr[100]; int rc; long t; if(argc > 1) { if(! (outFile = fopen(argv[1], "w"))) { perror(argv[1]); exit(1); } } else outFile = stdout; for(t=0; t<NTHREAD; t++) { fprintf(outFile, "In main: creating thread %ld\n", t); if(rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t)) { sprintf(errStr, "ERROR; pthread_create() returned %d", rc); perror(errStr); exit(2); } } pthread_exit(NULL); } 
+3
source share

The POSIX thread specification (AKA Pthreads) conforming to OS X requires stdio functions to be thread safe. It also provides flockfile and funlockfile functions to ensure that other threads cannot interleave I / O in FILE * while it is locked.

See http://pubs.opengroup.org/onlinepubs/007908799/xsh/threads.html , in particular, under the theme security topic .

+5
source share

All Articles