if (!flock($fp, LOCK_EX|LOCK_NB, $wouldblock)) {
if ($wouldblock) {
// another process holds the lock
}
else {
// couldn't lock for another reason, e.g. no such file
}
}
else {
// lock obtained
}
As described in the docs, use LOCK_NB
to make a non-blocking attempt to obtain the lock, and on failure check the $wouldblock
argument to see if something else holds the lock.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…