Since: 1.1.0

class SyncSharedMemory (View source)

SyncSharedMemory

A cross-platform, native, consistent implementation of named shared memory objects. Shared memory lets two separate processes communicate without the need for complex pipes or sockets. There are several integer-based shared memory implementations for PHP. Named shared memory is an alternative. Synchronization objects (e.g. SyncMutex) are still required to protect most uses of shared memory.

Methods

__construct(string $name, int $size)

Constructs a new SyncSharedMemory object

bool
first()

Check to see if the object is the first instance system-wide of named shared memory

string
read(int $start = 0, int $length)

Copy data from named shared memory

int
size()

Returns the size of the named shared memory

int
write(string $string, int $start = 0)

Copy data to named shared memory

Details

__construct(string $name, int $size)

Constructs a new SyncSharedMemory object

Constructs a named shared memory object.

Parameters

string $name

The name of the shared memory 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.

int $size

The size, in bytes, of shared memory to reserve. Note: The amount of memory cannot be resized later. Request sufficient storage up front.

Exceptions

Exception

bool first()

Check to see if the object is the first instance system-wide of named shared memory

Retrieves the system-wide first instance status of a SyncSharedMemory object.

Return Value

bool

TRUE if the object is the first instance system-wide, FALSE otherwise.

string read(int $start = 0, int $length)

Copy data from named shared memory

Copies data from named shared memory.

Parameters

int $start

[optional] The start/offset, in bytes, to begin reading. Note: If the value is negative, the starting position will begin at the specified number of bytes from the end of the shared memory segment.

int $length

[optional] The number of bytes to read. Note: If unspecified, reading will stop at the end of the shared memory segment. If the value is negative, reading will stop the specified number of bytes from the end of the shared memory segment.

Return Value

string

containing the data read from shared memory.

See also

SyncSharedMemory::write

int size()

Returns the size of the named shared memory

Retrieves the shared memory size of a SyncSharedMemory object.

Return Value

int

containing the size of the shared memory. This will be the same size that was passed to the constructor.

int write(string $string, int $start = 0)

Copy data to named shared memory

Copies data to named shared memory.

Parameters

string $string

The data to write to shared memoy. Note: If the size of the data exceeds the size of the shared memory, the number of bytes written returned will be less than the length of the input.

int $start

The start/offset, in bytes, to begin writing. Note: If the value is negative, the starting position will begin at the specified number of bytes from the end of the shared memory segment.

Return Value

int

containing the number of bytes written to shared memory.