php8.5
Home/ Manual/ sync / syncmutex/ SyncMutex::__construct

SyncMutex::__construct

PHP function Edit on GitHub ✎

(PECL sync >= 1.0.0)

Constructs a new SyncMutex object

Description

SyncMutex::__construct([string $name])

Constructs a named or unnamed countable mutex.

Parameters

name

The name of the mutex if this is a named mutex object.

Note

If the name already exists, it must be able to be opened by the current user that the process is running as or an exception will be thrown with a meaningless error message.

Return Values

The new SyncMutex object.

Errors/Exceptions

An exception is thrown if the mutex cannot be created or opened.

Examples

SyncMutex::__construct() named mutex with lock timeout example

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

if (!$mutex->lock(3000))
{
    echo "Unable to lock mutex.";

    exit();
}

/* ... */

$mutex->unlock();
?>

SyncMutex::__construct() unnamed mutex example

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

$mutex->lock();

/* ... */

$mutex->unlock();
?>

See Also

  • SyncMutex::lock
  • SyncMutex::unlock

Source: reference/sync/syncmutex/construct.xml · from the official PHP manual (php/doc-en)