hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - PHP框架 - YII - 正文 请牢记本站网址www.sosophp.cn

mutex yii怎么用_yii框架教程,mutex,yii

2020-02-03YII搜搜PHP网50°c
A+ A-

nginx不支持yii怎么办_yii框架教程

nginx不支持yii的解决办法:首先打开“nginx.config”配置文件;然后在文件里面增加“server”并且将“root”改成“index.php”文件所在目录;最后打开“web.php”文件并将访问权限设置成“777”即可。

mutex yii怎么用?Yii源码解析之Mutex

Mutex组件允许并发进程的相互执行,以防止“竞争条件”。这是通过使用“锁定”机制实现的。每个可能并发的线程通过在访问相应数据之前获取锁来进行协作。

相关推荐:yii教程

Yii2连接PostgreSql的方法_yii框架教程

本篇文章介绍了Yii2框架连接PostgreSql的方法,具有一定的参考价值,现在将它分享给各位,希望对各位有帮助!

用法示例:

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怎么用的详细内容,更多请关注ki4网其它相关文章!

yii与cms的区别_yii框架教程

CMS指的是内容管理系统,通常是开发好的web应用,放在服务器上就能使用的程序;Yii则是开发web应用的框架,使用它能够开发出CMS程序。

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
mutex yii怎么用_yii框架教程,mutex,yii

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签: