Fix multithread issues when operating on a single shared file