public function run() { // 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); }
/** * Release mutex * * Attempts to unlock the Mutex for the caller, optionally destroying the Mutex handle. * The calling thread should own the Mutex at the time of the call. * * @param int $mutex A handle returned by a previous call to Mutex::create(). * @param bool $destroy When true pthreads will destroy the Mutex after a successful unlock. * * @link http://www.php.net/manual/en/mutex.unlock.php * @return bool A boolean indication of success */ public static function unlock($mutex, $destroy = false) { if (fhread_mutex_unlock($mutex) === 0) { return true; } return false; }
/** * Executes given closure synchronized * * @param callable $sync */ public function synchronized(\Closure $sync) { fhread_mutex_lock($this->syncMutex); $sync->__invoke($this); fhread_mutex_unlock($this->syncMutex); }