title: How to make a file opened in C++ thread safe?
body: <p>Code snippet:</p>
  
  <pre><code>ofstream log;
  log.open("log.txt",ios::out);
  Thread threads[2];
  ...both threads write to the file
  log.close()
  </code></pre>
  
  <p>Any existing facility to lock the file when one thread writes to the file, so the two threads do not write at the same time. Thanks.</p>