I am trying to find a way to lock writing to a file in different threads. What is the correct way to do it? I would like not to open and close the file all the time when I n