PHP implementation read memory sequence number, PHP read memory sequence _php tutorial

Source: Internet
Author: User

PHP implementation read memory sequence number, PHP read memory order


Just do the recording, OSC this site should have duplicate

semWrapper.class.php

/* Signal Volume (Semaphore). * This is a wrapper class that addresses different implementations of "semaphores" under different platforms. * This class is only symbolic at the moment, and it is actually an empty run under the Windows platform (and does not really implement mutexes). */class semwrapper{  private $hasSemSupport;  Private $sem;  Const SEM_KEY = 1;   Public function __construct ()  {    $this->hassemsupport = function_exists (' Sem_get ');    if ($this->hassemsupport) {      $this->sem = Sem_get (Self::sem_key);    }  }   Public function Acquire () {    if ($this->hassemsupport) {      return Sem_acquire ($this->sem);    }    return true;  }   Public Function release () {    if ($this->hassemsupport) {      return sem_release ($this->sem);    }    return true;  }}

SeqGenerator.class.php

/* * Sequential number generator.   */class seqgenerator{Const SHM_KEY = 1;   /** * Initialize the sequence number generator.   * Only the first call after server startup is valid, and calling this method after this does not actually work.   * @param int $start The starting value of the sequence number.   * @return Boolean returns true to indicate success.    */static Public function init ($start = 1) {//mutual exclusion via semaphores, avoid access violation of shared memory $SW = new Semwrapper;    if (! $SW->acquire ()) {return false;    }//Open shared memory $shm _id = Shmop_open (Self::shm_key, ' n ', 0644, 4);      if (Empty ($shm _id)) {//due to the use of ' n ' mode, if the shared memory cannot be opened, you can assume that the shared memory has been created without having to initialize the $SW->release () again;    return true;    }//write the initial value in shared memory $size = Shmop_write ($shm _id, pack (' L ', $start), 0);      if ($size! = 4) {shmop_close ($shm _id);      $SW->release ();    return false;    }//Close shared memory, release semaphore Shmop_close ($shm _id);    $SW->release ();  return true;   }/** * produces the next sequential number.    * The order number generated by the @return int */static public function next () {//is mutually exclusive through semaphores to avoid access violation of shared memory $SW = new Semwrapper;    if (! $SW->acquire ()) {return 0; }     //Open Shared memory $shm _id = Shmop_open (Self::shm_key, ' W ', 0, 0);      if (Empty ($shm _id)) {$SW->release ();    return 0;    }//reads the sequence number from the shared memory $data = Shmop_read ($shm _id, 0, 4);      if (empty ($data)) {$SW->release ();    return 0;    } $arr = Unpack (' L ', $data);     $seq = $arr [1];    Write the next sequence number to the shared memory $size = Shmop_write ($shm _id, pack (' L ', $seq + 1), 0);      if ($size! = 4) {$SW->release ();    return 0;    }//Close shared memory, release semaphore Shmop_close ($shm _id);    $SW->release ();  return $seq; }}

page.php

Use method $seq = Seqgenerator::next (); Var_dump ($SEQ);

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/975524.html www.bkjia.com true http://www.bkjia.com/PHPjc/975524.html techarticle PHP Implementation read memory sequence number, PHP read memory order only to do record, OSC site should have a duplicate semWrapper.class.php/* * Semaphore (Semaphore). * This is a wrapper class for ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.