PHP 8.4.0 RC2 available for testing

SyncMutex::unlock

(PECL sync >= 1.0.0)

SyncMutex::unlockDestrava o mutex

Descrição

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

Diminui o contador interno de um objeto SyncMutex. Quando o contador interno atinge zero, o bloqueio real no objeto é liberado.

Parâmetros

all

Especifica se deve ou não definir o contador interno como zero e, portanto, liberar o bloqueio.

Valor Retornado

Retorna true em caso de sucesso ou false em caso de falha.

Exemplos

Exemplo #1 Exemplo de SyncMutex::unlock()

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

$mutex->lock();

/* ... */

$mutex->unlock();
?>

Veja Também

add a note

User Contributed Notes 1 note

up
4
npelov at croler dot net
4 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
To Top