Php reads the memory sequence number, and php reads the memory sequence. Php reads the memory sequence number. php reads the memory sequence only for recording. the osc site should have repeated semWrapper. class. php ** semaphores (Semaphore ). * This is a packaging class used for php to read the memory sequence number and php to read the memory sequence.
Only record, the osc site should have repeated
SemWrapper. class. php
/** Semaphore ). * This is a packaging class used to solve different implementation methods of the "Semaphore" on different platforms. * Currently, this class is symbolic. it is actually a dry run on Windows platforms (and does not actually implement mutual exclusion ). */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
/** Sequence number generator. */Class SeqGenerator {const SHM_KEY = 1;/*** initialize the sequence number generator. * This method is valid only when it is called for the first time after the server is started. * @ Param int $ start generates the starting value of the sequence number. * @ Return boolean true indicates success. */Static public function init ($ start = 1) {// mutex is implemented through semaphores to avoid access conflicts to shared memory $ sw = new SemWrapper; if (! $ Sw-> acquire () {return false;} // open the shared memory $ shm_id = shmop_open (self: SHM_KEY, 'n', 0644, 4 ); if (empty ($ shm_id) {// because the 'n' mode is used, if the shared memory cannot be enabled, you can think that the shared memory has been created, no need to initialize $ sw-> release (); return true;} // write the initial value $ size = shmop_write ($ shm_id, pack ('L ', $ start), 0); if ($ size! = 4) {shmop_close ($ shm_id); $ sw-> release (); return false;} // Close the shared memory and release the semaphores shmop_close ($ shm_id ); $ sw-> release (); return true;}/*** generates the next sequence number. * @ Return int the sequence number */static public function next () {// mutex through semaphores to avoid access conflicts to shared memory $ sw = new SemWrapper; if (! $ Sw-> acquire () {return 0;} // open the shared memory $ shm_id = shmop_open (self: SHM_KEY, 'W', 0, 0 ); if (empty ($ shm_id) {$ sw-> release (); return 0;} // read the sequence number $ 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 the shared memory and release the semaphores shmop_close ($ shm_id); $ sw-> release (); return $ seq ;}}
Page. php
// Usage method $ seq = SeqGenerator: next (); var_dump ($ seq );
The above is all the content of this article. I hope you will like it.
The timer is just a record. the osc site should have repeated semWrapper. class. php/** semaphores (Semaphore ). * This is a packaging class...