/*
*lock_thisfile: Get exclusive lock
* @param $tmpFileStr used as the file name of the shared lock file (can be a name randomly)
* @param $locktype lock type, the default is False (non-blocking, that is, once the lock fails to return false directly), set to True will wait until the lock succeeds to return
* @return If the lock succeeds, the lock instance is returned (this parameter is required when the Unlock_thisfile method is used), and the lock failure returns false.
*/
function Lock_thisfile ($tmpFileStr, $locktype =false) {
if ($locktype = = False)
$locktype = lock_ex| LOCK_NB;
$can _write = 0;
$LOCKFP = @fopen ($tmpFileStr. ". Lock "," w ");
if ($LOCKFP) {
$can _write = @flock ($LOCKFP, $locktype);
}
if ($can _write) {
return $LOCKFP;
}
else{
if ($LOCKFP) {
@fclose ($LOCKFP);
@unlink ($tmpFileStr. ". Lock ");
}
return false;
}
}
/**
*unlock_thisfile: Unlocking An instance of a previously acquired lock
* @param $fp The return value of the Lock_thisfile method
* @param $tmpFileStr used as the file name of the shared lock file (can be a name randomly)
*/
function Unlock_thisfile ($fp, $tmpFileStr) {
@flock ($fp, Lock_un);
@fclose ($FP);
@fclose ($FP);
@unlink ($tmpFileStr. ". Lock ");
}
?>
Examples of Use
$TMPFILESTR = "/tmp/mylock.loc";
Wait for the operation permission, if you want to return immediately, set the second argument to false.
$lockhandle = Lock_thisfile ($tmpFileStr, true);
if ($lockhandle) {
All transactions that require exclusive processing are performed here.
// ... ...
The transaction has finished processing.
Unlock_thisfile ($lockhandle, $TMPFILESTR);
}
?>
http://www.bkjia.com/PHPjc/629786.html www.bkjia.com true http://www.bkjia.com/PHPjc/629786.html techarticle PHP/* *lock_thisfile: Get exclusive lock * @param $tmpFileStr used as the file name of the shared lock file (can be any name) * @param $locktype lock type, the default is False (non ...)