首頁 > php框架 > YII > 主體

mutex yii怎麼用

藏色散人
發布: 2020-02-03 10:19:08
原創
2974 人瀏覽過

mutex yii怎麼用

mutex yii怎麼用? Yii原始碼解析之Mutex

Mutex元件允許並發進程的相互執行,以防止「競爭條件」。這是透過使用“鎖定”機制來實現的。每個可能並發的執行緒透過在存取對應資料之前獲取鎖來進行協作。

相關推薦:yii教學

用法範例:

if ($mutex->acquire($mutexName)) {
    // business logic execution
} else {
    // execution is blocked!
}
登入後複製

這是一個基類,應該擴展它以實現實際的鎖定機制。

下面我們看看其原始碼實現:

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
 
namespace yii\mutex;
 
use yii\base\Component;
 
/**
 * The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions".
 *
 * This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring
 * a lock before accessing the corresponding data.
 *
 * Usage example:
 *
 * ```
 * if ($mutex->acquire($mutexName)) {
 *     // business logic execution
 * } else {
 *     // execution is blocked!
 * }
 * ```
 *
 * This is a base class, which should be extended in order to implement the actual lock mechanism.
 *
 * @author resurtm <resurtm@gmail.com>
 * @since 2.0
 */
abstract class Mutex extends Component
{
    /**
     * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically
     * before finishing script execution. Defaults to true. Setting this property to true means that all locks
     * acquired in this process must be released (regardless of errors or exceptions).
     */
    public $autoRelease = true;//是否自动释放锁
 
    /**
     * @var string[] names of the locks acquired by the current PHP process.
     */
    private $_locks = [];//当前进程拥有的锁信息
 
 
    /**
     * 初始化Mutex组件
     */
    public function init()
    {
        if ($this->autoRelease) {//如果是自动释放锁
            $locks = &$this->_locks;
            //注册shutdown回调函数,在这里做锁的释放动作
            register_shutdown_function(function () use (&$locks) {
                foreach ($locks as $lock) {
                    $this->release($lock);
                }
            });
        }
    }
 
    /**
     * Acquires a lock by name.
     * @param string $name of the lock to be acquired. Must be unique.
     * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return
     * false immediately in case lock was already acquired.
     * @return bool lock acquiring result.
     */
    public function acquire($name, $timeout = 0)//按名称获取锁
    {
        if ($this->acquireLock($name, $timeout)) {
            $this->_locks[] = $name;
 
            return true;
        }
 
        return false;
    }
 
    /**
     * Releases acquired lock. This method will return false in case the lock was not found.
     * @param string $name of the lock to be released. This lock must already exist.
     * @return bool lock release result: false in case named lock was not found..
     */
    public function release($name)//按名称释放锁
    {
        if ($this->releaseLock($name)) {
            $index = array_search($name, $this->_locks);
            if ($index !== false) {
                unset($this->_locks[$index]);
            }
 
            return true;
        }
 
        return false;
    }
 
    /**
     * This method should be extended by a concrete Mutex implementations. Acquires lock by name.
     * @param string $name of the lock to be acquired.
     * @param int $timeout time (in seconds) to wait for the lock to be released.
     * @return bool acquiring result.
     */
    abstract protected function acquireLock($name, $timeout = 0);//抽象函数,按名称获取锁
 
    /**
     * This method should be extended by a concrete Mutex implementations. Releases lock by given name.
     * @param string $name of the lock to be released.
     * @return bool release result.
     */
    abstract protected function releaseLock($name);//抽象函数,按名称释放锁
}
登入後複製

其作為基礎類,子類需要實現acquireLock和releaseLock方法,即具體的加鎖和解鎖策略。

以上是mutex yii怎麼用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板