I found at the flock manual the following description:
By default, this function will block until the requested lock is acquired
Further below I found the following example code:
<?php
$fp = fopen("/tmp/lock.txt", "r+");
if (flock($fp, LOCK_EX)) { // acquire an exclusive lock
ftruncate($fp, 0); // truncate file
fwrite($fp, "Write something here\n");
fflush($fp); // flush output before releasing the lock
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't get the lock!";
}
fclose($fp);
?>
But is there any case where the script would actually return "Couldn't get the lock!"
? I thought it waits until the file lock.txt
gets unlocked. If the file never gets unlocked, then the script waits forever, right?
Further, I found this answer explaining the difference between exclusive and shared lock on unix: https://stackoverflow.com/a/11837714/2311074 Do these 4 rules also apply to flock in PHP (for example "If one or more shared locks already exist, exclusive locks cannot be obtained")?