互斥yii的使用方法

  介绍

了解互斥yii的使用方法?这个问题可能是我们日常学习或工作经常见到的,希望通过这个文章能让大家收获颇深,下面是小编给大家带来的参考内容,让我们一起来看看吧!

<强>互斥yii怎么用? yii源码解析之互斥

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

相关推荐:yii教程

用法示例:

if (互斥→美元收购(mutexName美元)),{   ,,,//,business  logic 执行   },{else    ,,,//,execution  is 阻塞!   }

这是一个基类,应该扩展它以实现实际的锁机制。

下面我们看看其源码实现:

& lt; php ?/* *   ,* @link  http://www.yiiframework.com/,* @copyright  Copyright  (c), 2008年,Yii  Software 有限责任公司   ,* @license  http://www.yiiframework.com/license/,*/,   namespace , yii \互斥对象;   ,   use  yii \基地\组件;   ,/* *   ,*,Mutex  component  allows  mutual  execution  of  concurrent  processes 拷贝order 用prevent “race  conditions"。   ,*   ,*却;能够is  achieved  by  using  a “lock",机制只Each  possibly  concurrent  thread  cooperates  by 收购   ,* a  lock  before  accessing 从而corresponding 数据。   ,*   ,* Usage 例如:   ,*   ,* ' ' '   ,* if (互斥→美元收购(mutexName美元)),{   ,*,,,,//,business  logic 执行   ,*},{else    ,*,,,,//,execution  is 阻塞!   ,*}   ,* ' ' '   ,*   ,*却;能够is  a  base 类,,which  should  be  extended 拷贝order 用implement 从而actual  lock 机制。   ,*   ,* @author  resurtm  & lt; resurtm@gmail.com>   ,* @since  2.0   ,*/abstract  class  Mutex  extends 组件   {   ,,,/* *   ,,,,*,@var  bool  whether  all  locks  acquired 却;能够拷贝process (即只local 锁),must  be  released 自动   ,,,,*,before  finishing  script 执行只Defaults 用真正的只Setting 却;能够property 用true  means  that  all 锁   ,,,,*,acquired 却;能够拷贝process  must  be  released  (regardless  of  errors 或是例外)。   ,,,*/,,,public  autoRelease 美元;=,真的,//是否自动释放锁   ,   ,,,/* *   ,,,,*,@var  string [], names  of 从而locks  acquired  by 从而current  PHP 过程。   ,,,*/,,,private  _locks 美元;=,[];//当前进程拥有的锁信息   ,   ,   ,,,/* *   ,,,,*,初始化互斥组件   ,,,*/,,,public  function  init ()   ,,,{   ,,,,,,,if  ($ this→autoRelease),{//如果是自动释放锁   ,,,,,,,,,,,locks 美元;=,,$ this→_locks;   ,,,,,,,,,,,//注册关闭回调函数,在这里做锁的释放动作   ,,,,,,,,,,,register_shutdown_function (function  (), use (和锁美元),{   ,,,,,,,,,,,,,,,foreach  (locks 美元;as 锁定美元),{   ,,,,,,,,,,,,,,,,,,,这个美元→释放锁($);   ,,,,,,,,,,,,,,,}   ,,,,,,,,,,,});   ,,,,,,,}   ,,,}   ,   ,,,/* *   ,,,,*,Acquires  a  lock  by 名字。   ,,,,*,@param  string  name 美元;of 从而lock 用be 收购只Must  be 独一无二的。   ,,,,*,@param  int  timeout 美元;time (秒)拷贝,用wait  for  lock 用be 只公布Defaults 用zero  meaning  that  method  will 回报   ,,,,*,false  immediately 拷贝case  lock  was  already 收购。   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

互斥yii的使用方法