我在運行Windows XP SP3和Apache 2.2.21的Web服務器上使用PHP 5.3.8,我需要創建一個互斥鎖。經過一番研究,我已經遇到了flock命令像這樣實現的:互斥與PHP 5.3.8
class Mutex
{
private $lock_ = null;
// create a mutex with with a given id. This ID must be system unique.
// [string] id - a unique id
// return - true on success
public function Initialize($id)
{
$this->lock_ = fopen($id, 'a');
return is_resource($this->lock_);
}
// destroy the mutex
public function Destroy()
{
$result = false;
if (is_resource($this->lock_));
{
$result = flock($this->lock_, LOCK_UN);
$result &= fclose($this->lock_);
$this->lock_ = null;
}
return $result;
}
// exclusively lock the resource
// return - true on success
public function Lock()
{
if (is_resource($this->lock_))
return flock($this->lock_, LOCK_EX);
return false;
}
// release the locked resource
// return - true on success
public function Release()
{
if (is_resource($this->lock_))
return flock($this->lock_, LOCK_UN);
return false;
}
}
但是,當我去使用這個類:
$this->cache_lock_ = new Mutex();
$this->cache_lock_->Initialize("e:\\cache_lock");
if ($this->cache_lock_->Lock())
echo "Acquired 1 ".PHP_EOL;
if ($this->cache_lock_->Lock())
echo "Acquired 2 ".PHP_EOL;
$this->cache_lock_->Release();
$this->cache_lock_->Destroy();
我看到Acquired 1 Acquired 2
印刷指示鎖是儘管我指定它是排他性的,但獲得了兩次。
有人可以建議我做錯了什麼嗎?理想情況下,我希望第二個Lock()調用直到資源可用。
感謝, PaulH
你在'Destroy'中有'if(is_resource($ this-> lock _))';''看起來不應該有分號。另外,如果您使用的是PHP5(並且您應該)考慮使用'__construct'和'__destruct' PHP魔術方法,而不是'Initialize'和'Destroy'。 – 2013-10-12 21:03:57