class SyncMutex (View source)

SyncMutex

A cross-platform, native implementation of named and unnamed countable mutex objects. A mutex is a mutual exclusion object that restricts access to a shared resource (e.g. a file) to a single instance. Countable mutexes acquire the mutex a single time and internally track the number of times the mutex is locked. The mutex is unlocked as soon as it goes out of scope or is unlocked the same number of times that it was locked.

Methods

__construct(string $name)

Constructs a new SyncMutex object

bool
lock(int $wait = -1)

Waits for an exclusive lock

bool
unlock(bool $all = false)

Unlocks the mutex

Details

__construct(string $name)

Constructs a new SyncMutex object

Constructs a named or unnamed countable mutex.

Parameters

string $name

[optional] The name of the mutex if this is a named mutex object. 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.

Exceptions

Exception

bool lock(int $wait = -1)

Waits for an exclusive lock

Obtains an exclusive lock on a SyncMutex object. If the lock is already acquired, then this increments an internal counter.

Parameters

int $wait

[optional] The number of milliseconds to wait for the exclusive lock. A value of -1 is infinite.

Return Value

bool

TRUE if the lock was obtained, FALSE otherwise.

See also

SyncMutex::unlock

bool unlock(bool $all = false)

Unlocks the mutex

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

Parameters

bool $all

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

Return Value

bool

TRUE if the unlock operation was successful, FALSE otherwise.

See also

SyncMutex::lock