Is fprintf() thread safe?
C2011 -- the first version of the standard to acknowledge the existence of threads in the first place -- places no limitation on how fprintf()
calls in different threads may or do interact. In that sense, fprintf()
is not thread-safe.
POSIX, however, does specify that fprintf()
calls from different threads of the same process do not interfere with each other, and if that if they both specify the same target file, their output will not be intermingled. POSIX-conforming fprintf()
is thus thread-safe in that sense.
I cannot speak to whether standard C++ places requirements that have the effect of requiring fprintf()
to be thread safe. I would find that surprising, but it could be true. To be sure, it is safe to write to an iostream
object from multiple threads, but that does not imply that the same is true of fprintf()
.
But none of that really matters if you're asking about Windows C or C++, however, which (the C in particular) are well known to be non-conforming. If you want to know about Windows's fprintf()
in particular, then that has already been answered here (yes).