title: Prevent multiple threads writing the same file
body: <p>i am designing and developing an api where multiple threads are downloading files from the net and then write it to disk.</p>
  
  <p>if it is used incorrectly it could happen that the same file is downloaded and written by more than one threads, which will lead to an exception at the moment of writing to disk.</p>
  
  <p>i would like to avoid this problem with a <code>lock() { ... }</code> around the part that writes the file, but obviously i dont want to lock with a global object, just something that is related to that specific file so that not all threads are locked when a file is written.</p>
  
  <p>i hope this question is understandable.</p>