Ejemplo n.º 1
0
 /**
  * Create, and optionally lock a new Mutex for the caller
  *
  * @param bool $lock Setting lock to true will lock the Mutex for the caller before returning the handle
  *
  * @link http://www.php.net/manual/en/mutex.create.php
  * @return int A newly created and optionally locked Mutex handle
  */
 public static function create($lock = false)
 {
     return fhread_mutex_init();
 }
Ejemplo n.º 2
0
    {
        // lock data object
        fhread_mutex_lock($this->data->mutex);
        // inc counter
        ++$this->data->counter;
        // add message that this thread was here
        $this->data->objects[$this->getThreadId()] = new stdClass();
        // unlock data object
        fhread_mutex_unlock($this->data->mutex);
    }
}
// create data storage object
$data = new \stdClass();
$data->objects = array();
// init mutex
$data->mutex = fhread_mutex_init();
// init counter property to be zero
$data->counter = 0;
// init threads array
$ths = array();
// define max threads
$tMax = 1000;
// initiate threads
for ($i = 1; $i <= $tMax; $i++) {
    $ths[$i] = new CounterThread($data);
}
// start threads
for ($i = 1; $i <= $tMax; $i++) {
    $ths[$i]->start();
}
// start threads
Ejemplo n.º 3
0
 /**
  * Start method which will prepare, create and starts a thread
  *
  * @return boolean pthread create state
  */
 public function start()
 {
     // check if was started already
     if ($this->getState() >= self::STATE_STARTED && $this->getState() < self::STATE_JOINED) {
         throw new \Exception('Thread has been started already!');
     }
     $this->setState(self::STATE_STARTED);
     // init thread cond
     $this->syncNotify = fhread_cond_init();
     // init thread mutex
     $this->globalMutex = fhread_mutex_init();
     $this->stateMutex = fhread_mutex_init();
     $this->syncMutex = fhread_mutex_init();
     // create, start thread and save thread id
     if (fhread_create($this, $this->threadId, $this->fhreadHandle) === 0) {
         $this->setState(self::STATE_RUNNING);
         return true;
     }
     $this->setState(self::STATE_ERROR);
     return false;
 }