Question

I'm having trouble with implementation of shared memory in php. Is it possible to make queue in shared memory?

Code is here:

// queue class
class AddressQueue extends \Nette\Object {

private $queue = array();

public function add($var) {
    $this->queue[] = $var;
}

public function get() {
    return array_shift($this->queue);
}

public function num() {
    return count($this->queue);
}
}

$segmentKey = "987654";
$permissions = 0666;
$size = 8092;
$sharedSegment = shm_attach($segmentKey, $size, $permissions);

shm_put_var($sharedSegment, QUEUE_INDEX, new AddressQueue());

...at this point, how can I add an item (int or very short string) to queue? Is it even possible?

Was it helpful?

Solution

Well you would get it back out using shm_get_var(), but there's no point in doing that, just add it before you use shm_put_var() like so:

$addrQueue = new AddressQueue();
$addrQueue->add('whatever');
shm_put_var($sharedSegment, QUEUE_INDEX, $addrQueue);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top