企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Lock [TOC] `1.6.4`版本增加了锁的实现。`PHP`代码中可以很方便地创建一个锁,用来实现数据同步。`Lock`类支持`5`种锁的类型: 1. 文件锁`SWOOLE_FILELOCK` 2. 读写锁`SWOOLE_RWLOCK` 3. 信号量`SWOOLE_SEM` 4. 互斥锁`SWOOLE_MUTEX` 5. 自旋锁`SWOOLE_SPINLOCK` **注意:请勿在`onReceive`等回调函数中创建锁,否则底层的`GlobalMemory`内存会持续增长,造成内存泄漏。** ## 示例: ~~~ $lock = new swoole_lock(SWOOLE_MUTEX); echo "[Master]create lock\n"; $lock->lock(); if (pcntl_fork() > 0) { sleep(1); $lock->unlock(); } else { echo "[Child] Wait Lock\n"; $lock->lock(); echo "[Child] Get Lock\n"; $lock->unlock(); exit("[Child] exit\n"); } echo "[Master]release lock\n"; unset($lock); sleep(1); echo "[Master]exit\n"; ~~~