略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: SyncMutex::unlock

2024-12-20

SyncMutex::unlock

(PECL sync >= 1.0.0)

SyncMutex::unlockUnlocks the mutex

说明

public SyncMutex::unlock(bool $all = false): bool

Decreases the internal counter of a SyncMutex object. When the internal counter reaches zero, the actual lock on the object is released.

参数

all

Specifies whether or not to set the internal counter to zero and therefore release the lock.

返回值

成功时返回 true, 或者在失败时返回 false

范例

示例 #1 SyncMutex::unlock() example

<?php
$mutex 
= new SyncMutex("UniqueName");

$mutex->lock();

/* ... */

$mutex->unlock();
?>

参见

add a noteadd a note

User Contributed Notes 1 note

up
1
npelov at croler dot net
2 years ago
If the php process dies unexpectedly (on linux) the lock will remain with no option to remove it. unlock() will not work if the lock is being held by other process. SyncMutex uses shared mem. in that case you can delete the shared memory object in /dev/shm. To list the objects:

ls /dev/shm/Sync_Mutex*

Keep in mind that objects remain there even after unlocking. So if you use lots of random mutex names then the number of these objects will grow and and you might want to cleanup at some point

Example sync.php:
<?php

$s
= new SyncMutex("mymutex");

if(!
$s->lock()){
  echo
"cannot lock\n";
  exit;
}
echo
"locked\n";
sleep(10);
$s->unlock();
echo
"unlocked\n";
?>

then run:

php -f sync.php

and press ctrl-c. If you run it again it won't lock. So what you can do is:

rm -f /dev/shm/Sync_Mutex-*-mymutex

Then run it again - it'll lock again

官方地址:https://www.php.net/manual/en/syncmutex.unlock.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3