国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 > php框架 > YII > 正文

mutex yii怎么用

藏色散人
發(fā)布: 2020-02-03 10:19:08
原創(chuàng)
3113人瀏覽過

mutex yii怎么用

mutex yii怎么用?yii源碼解析之mutex

Mutex組件允許并發(fā)進(jìn)程的相互執(zhí)行,以防止“競爭條件”。這是通過使用“鎖定”機(jī)制實現(xiàn)的。每個可能并發(fā)的線程通過在訪問相應(yīng)數(shù)據(jù)之前獲取鎖來進(jìn)行協(xié)作。

相關(guān)推薦:yii教程

用法示例:

if ($mutex->acquire($mutexName)) {
    // business logic execution
} else {
    // execution is blocked!
}
登錄后復(fù)制

這是一個基類,應(yīng)該擴(kuò)展它以實現(xiàn)實際的鎖機(jī)制。

下面我們看看其源碼實現(xiàn):

<?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 = [];//當(dāng)前進(jìn)程擁有的鎖信息
 
 
    /**
     * 初始化Mutex組件
     */
    public function init()
    {
        if ($this->autoRelease) {//如果是自動釋放鎖
            $locks = &$this->_locks;
            //注冊shutdown回調(diào)函數(shù),在這里做鎖的釋放動作
            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);//抽象函數(shù),按名稱獲取鎖
 
    /**
     * 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);//抽象函數(shù),按名稱釋放鎖
}
登錄后復(fù)制

其作為基礎(chǔ)類,子類需要實現(xiàn)acquireLock和releaseLock方法,即具體的加鎖和解鎖策略。

以上就是mutex yii怎么用的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!

最佳 Windows 性能的頂級免費優(yōu)化軟件
最佳 Windows 性能的頂級免費優(yōu)化軟件

每個人都需要一臺速度更快、更穩(wěn)定的 PC。隨著時間的推移,垃圾文件、舊注冊表數(shù)據(jù)和不必要的后臺進(jìn)程會占用資源并降低性能。幸運的是,許多工具可以讓 Windows 保持平穩(wěn)運行。

下載
相關(guān)標(biāo)簽:
來源:php中文網(wǎng)
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn
最新問題
開源免費商場系統(tǒng)廣告
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號