title: Why is locking such a mess in PHP?
body: <p>A SO user asked <a href="https://stackoverflow.com/questions/3990079/execute-php-script-only-once-on-live-site">a question</a> to which the answer effectively was "use a locking mechanism".</p>
  
  <p>While researching my answer, I discovered that there seems to be no simple, inter-process-reliable locking mechanism in PHP. <a href="http://php.net/flock" rel="nofollow noreferrer">flock()</a> has a big fat warning:</p>
  
  <blockquote>
    <p>On some operating systems flock() is implemented at the process level. When using a multithreaded server API like ISAPI you may not be able to rely on flock() to protect files against other PHP scripts running in parallel threads of the same server instance!</p>
  </blockquote>
  
  <p>The discussion in <a href="https://stackoverflow.com/questions/3759345/fopen-file-locking-in-php-reader-writer-type-of-situation">this question</a> delves into the issue pretty deeply, but comes up only with rather complex solutions: Using a RAM disk, or Memcache. </p>
  
  <p>The only thing that looks halfway good is mySQL's <a href="https://stackoverflow.com/questions/3446395/how-can-i-create-a-non-file-lock-in-php"><code>GET_LOCK()</code>.</a></p>
  
  <p>So my question is: Is this really the way it is? Is there really no simple,  straightforward, cross-platform safe locking system in PHP? One that is atomic, and will release the lock if the owner process dies, and doesn't need huge setup efforts?  </p>