fprintf - threadsafe? - i.e. with process linked against '-pthread'?

Karl Pielorz kpielorz_lst at tdx.co.uk
Wed Oct 11 09:41:32 UTC 2017


Hi,

I have a number of 10.3-R amd64 boxes which runs a heavily threaded 
process. This is linked against '-pthread' - and compiles / runs fine.

Using 'fprintf' to log data to a file - it sometimes doesn't complete 
writing the line - e.g. literally in code:

   fprintf( fd, "The quick brown %s jumped over the slow lazy animal\n", 
animal );

Will sometimes result in:

"
The quick brown fox ju"

Being written to the file.


Presumably (and from what I can see) fprintf is 'thread safe'? - And it 
also appears multiple threads could write to a single file using it (i.e. 
it provides for atomic writes so lines won't intermingle - the lines 
written don't seem to intermingle).

The process doesn't crash - but I can't understand why / how frpintf could 
either stop, or get stopped 'mid way' through?

e.g. If a signal occurred would it complete the write to file?

This only happens very, very occasionally (one fprintf out of many 
millions, with hundreds of threads running).

Just a bit stumped as to what to try looking at to fix / debug the problem 
- if anyone has any suggestions, or further reading I can look at.

-Karl


More information about the freebsd-hackers mailing list