You can try to use flock() to lock file.
In reply to Re: making sure a file is not currently being written to by Gangabass in thread making sure a file is not currently being written to by why_bird