Just because I *love* flogging a dead horse, here's a link to GNU's glibc fflush implementation with locking (I think condition on pthreads being linked): https://sourceware.org/git/?p=glibc.git;a=blob;f=libio/iofflush.c;h=5205d10604db395d9aa9f05b6b32991728644349;hb=HEAD#l40