2

I was wondering whether this is a good way to implement mutual exclusion on PHP: You use PHP flock() and a dummy lock file.

Because flock() only allows one process to access a file.

$file = fopen("dummyLockFile");
flock($file, LOCK_EX)
//Critical section
fflock($file, LOCK_UN)
fclose($file)
Long Le
  • 404
  • 5
  • 18
  • I'm curious too; this is extremely relevant to an issue I've been having http://stackoverflow.com/questions/36657155/files-disappearing-in-the-middle-of-a-function – David Wyly Apr 21 '16 at 23:07
  • Yes. It's not perfect, but that's because PHP was not designed with threading, multiprocess, or IPC in mind. – Sammitch Apr 21 '16 at 23:50
  • If you're on Windows you can [use Notepad to lock a file](http://superuser.com/a/649819/496195). – ourmandave Apr 22 '16 at 00:26

0 Answers0