title: Why fread does have thread safe requirements which slows down its call
body: <p>I am writing a function to read binary files that are organized as a succession of (key, value) pairs where keys are small ASCII strings and value are int or double stored in binary format.</p>
  
  <p>If implemented naively, this function makes a lot of call to fread to read very small amount of data (usually no more than 10 bytes). Even though fread internally uses a buffer to read the file, I have implemented my own buffer and I have observed speed up by a factor of 10 on both Linux and Windows. The buffer size used by fread is large enough and the function call cannot be responsible for such a slowdown. So I went and dug into the GNU implementation of fread and discovered some lock on the file, and many other things such as verifying that the file is open with read access and so on. No wonder why fread is so slow.</p>
  
  <p>But what is the rationale behind fread being thread-safe where it seems that multiple thread can call fread on the <em>same</em> file which is mind boggling to me. These requirements make it slow as hell. What are the advantages?</p>