入门指引 Server Client Process AsyncIO Memory HttpServer WebSocket 高级 其他

发布于 2015-08-09 09:33:29 | 489 次阅读 | 评论: 0 | 来源: 网络整理

swoole1.6.4版本增加了锁的实现。PHP代码中可以很方便地创建一个锁,用来实现数据同步。swoole_lock类支持5种锁的类型:

  1. 文件锁 SWOOlE_FILELOCK
  2. 读写锁 SWOOLE_RWLOCK
  3. 信号量 SWOOLE_SEM
  4. 互斥锁 SWOOLE_MUTEX
  5. 自旋锁 SWOOlE_SPINLOCK

示例:

$lock = new swoole_lock(SWOOLE_MUTEX);
echo "[Master]create lockn";
$lock->lock();
if(pcntl_fork() > 0) 
{
        sleep(1);
        $lock->unlock();
} 
else
{
        echo "[Child] Wait Lockn";
        $lock->lock();
        echo "[Child] Get Lockn";
        $lock->unlock();
        exit("[Child] exitn");
}
echo "[Master]release lockn";
unset($lock);
sleep(1);
echo "[Master]exitn";

子目录

swoole_lock->__construct 
swoole_lock->lock 
swoole_lock->trylock 
swoole_lock->unlock 
swoole_lock->lock_read 
swoole_lock->trylock_read 
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务