title: fopen/fwrite and multi-threading?
body: <p>fopen/fwrite and multi-threading?</p>
  
  <p>Some multi-threading programs open the same file, each thread create a file pointer to that the file.</p>
  
  <p>There is one thread created by a paricular program that will update the file at some random time, whilst other threads, created by a different program, will simply read the contents of the file.</p>
  
  <p>I guess this create a racing/data-inconsistence problem there if the writing thread change contents in the file whilst other threads try to read the contents. </p>
  
  <p>The problem here is the thread that update the file should compiled into a different exe program than the the program that creates threads that read the contents of the file, so within-program level thread control become impossible.</p>
  
  <p>My solution is create a very small "flag" file on the harddisk to indicates 3 status of the file: </p>
  
  <p>1) writing-thread is updating the contents of the file;</p>
  
  <p>2) reading-thread are reading the contents of the file;</p>
  
  <p>3) Neither 1) or 2);</p>
  
  <p>Using this flag file to block threads whenever necessary.</p>
  
  <p>Are there some more-compact/neat solution to this problem? </p>