It depends on the file writing pattern you have in your program:
- If you write not so often (compared to the amount of computation you do) you could just protect file writing code with a mutex
- If writing happens more often you could us a separate file for each thread and then merge them in the final file
You could also make a queue of data to be written in which scenario computation threads would be producers and there would be a single thread writing data to disk. You would probably need some kind of a queue size control for this if writing consumer can not keep up with producers.
Scheme with a queue is also nice because it separates computation and IO improving modularity.