PHP code example of takuya / php-sysv-ipc-semaphore

1. Go to this page and download the library: Download takuya/php-sysv-ipc-semaphore library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

takuya / php-sysv-ipc-semaphore example snippets



$uniq_name = 'semphore_name';
$semaphore = new IPCSemaphore($uniq_name);
$semaphore->acquire();// first acquire must be success.
$semaphore->acquire(true);// multiple acquired result true
$semaphore->release();
//
// remove from IPC
//
$semaphore->destroy();


$semaphore = new IPCSemaphore('semphore_name');
$ret = $semaphore->withLock(function(){
   // do something in lock
   echo "run in lock";
   return 1234;
});
$ret === 1234; //=> true


function RunWithLock(){
  $semaphore = new IPCSemaphore('sem_name');
  // $lock is local scope.
  // auto released by destructor on garbage collection.
  $lock = $semaphore->lock();
  return 1234;
}
/// 
RunWithLock();


function sample($msg){
  try {
    $sem = new IPCSemaphore(str_rand(10));
    $sem->acquire(); 
    return $msg; // finally called before return.
  } finally {
    $sem->release();
  }
}
shell
name='php-sysv-ipc-semaphore'
composer config repositories.$name \
vcs https://github.com/takuya/$name  
composer