/** * @expectedException RuntimeException */ public function testThreadAlreadyJoined() { $thread = new Thread(); $this->assertEquals($thread->start(), true); $this->assertEquals($thread->join(), true); $this->assertEquals($thread->join(), false); }
/** * @expectedException RuntimeException */ public function testThreadAlreadyJoined() { $thread = new Thread(); $this->assertTrue($thread->start()); $this->assertTrue($thread->join()); $this->assertFalse($thread->join()); }
public function start($options = PTHREADS_INHERIT_ALL) { ThreadManager::getInstance()->add($this); if (!$this->isRunning() and !$this->isJoined() and !$this->isTerminated()) { return parent::start($options); } return false; }
/** * Starts new threads if needed * * @return int queue size */ public function tick() { $this->cleanup(); if (count($this->threads) < $this->queueSize && count($this->jobs)) { $this->threads[] = $szal = new Thread($this->callable); $szal->start(array_shift($this->jobs)); } usleep(ThreadQueue::TICK_DELAY); return $this->queueSize(); }
function run() { while (true != $this->exit) { $sock = socket_accept($this->ls); if ($sock) { $hs = new Thread(new HttpHandler($sock, $this->handler)); $hs->start(); } } }
static function start($transport, $nodename = null, $background = true) { $node = new LdwpNode(); if ($background) { $t = new Thread($node); $pid = $t->start(); console::writeLn("Forked with pid %d", $pid); } else { $node->threadmain(); } }
public function start(int $options = PTHREADS_INHERIT_ALL) { ThreadManager::getInstance()->add($this); if (!$this->isRunning() and !$this->isJoined() and !$this->isTerminated()) { if ($this->getClassLoader() === null) { $this->setClassLoader(); } return parent::start($options); } return false; }
public function handle(\Thread $thread) { $thread->start(); echo "Start thread with ID {$thread->getCurrentThreadId()}\n"; return Observable::create(function (ObserverInterface $observer) use($thread) { while ($thread->isRunning()) { $this->loop->tick(); } try { echo "Thread finished\n"; $thread->join(); $observer->onNext(new Event('/thread/ok', $thread)); $observer->onCompleted(); } catch (\Exception $e) { echo "Thread error\n"; $observer->onError($e); } unset($thread); }); }
/** * {@inheritdoc} */ protected function spawnWorker() : Awaitable { if (self::$sharedWorkers === null) { self::$sharedWorkers = []; \register_shutdown_function(function () { self::shutdownPool(); }); } $index = self::$allocated++ % self::getMaxSize(); if (isset(self::$sharedWorkers[$index])) { return new Success(self::$sharedWorkers[$index]); } try { list($a, $b) = Socket::createPair(); $thread = new Thread($b, self::getComposerAutoloader()); $thread->start(\PTHREADS_INHERIT_INI); return new Success(self::$sharedWorkers[$index] = new ThreadWorker($index, $a, $thread)); } catch (\Throwable $e) { self::$allocated--; return new Failure($e); } }
<?php require "thread.php"; echo "Hola que talllll"; function proceso($tiempo, $resultado) { usleep($tiempo); exit($resultado); } $thread1 = new Thread('proceso'); $thread2 = new Thread('proceso'); $thread3 = new Thread('proceso'); $thread1->start(3, 10); $thread2->start(2, 40); $thread3->start(1, 30); while ($thread1->isAlive() || $thread2->isAlive() || $thread3->isAlive()) { } echo "Resultado del hilo 1 (debe ser 3): " . $thread1->getExitCode() . "\n"; echo "Resultado del hilo 2 (debe ser 2): " . $thread2->getExitCode() . "\n"; echo "Resultado del hilo 3 (debe ser 1): " . $thread3->getExitCode() . "\n";
/** * Start the thread * * @throws \RuntimeException */ public function execute() { $this->args = func_get_args(); return parent::start(); }
public final function start($options = PTHREADS_INHERIT_ALL) { ThreadManager::getInstance()->add($this); return parent::start($options); }
<?php /** * This file serves as a benchmark for pthreads initialization/destruction * usage: php-zts examples/Benchmark.php [threads] [samples] * threads - the number of threads to create, default=100 * samples - the number of times to run the test, default=5 */ /** * Nothing */ $max = @$argv[1] ? $argv[1] : 100; $sample = @$argv[2] ? $argv[2] : 5; printf("Start(%d) ...", $max); $it = 0; do { $s = microtime(true); /* begin test */ $ts = []; while (count($ts) < $max) { $t = new Thread(); $t->start(); $ts[] = $t; } $ts = []; /* end test */ $ti[] = $max / (microtime(true) - $s); printf("."); } while ($it++ < $sample); printf(" %.3f tps\n", array_sum($ti) / count($ti));
private function spawnWorker() { $results = new SharedData(); $resultCodes = new SharedData(); $thread = new Thread($results, $resultCodes, $this->ipcUri); if (!$thread->start()) { throw new \RuntimeException('Worker thread failed to start'); } $worker = new Worker(); $worker->id = $thread->getThreadId(); $worker->results = $results; $worker->resultCodes = $resultCodes; $worker->thread = $thread; $this->pendingWorkers[$worker->id] = $worker; $this->pendingWorkerCount++; return $worker; }
/** * Spawns the module's logic in a new worker thread. */ public function start() { $stopRequested = false; parent::start(PTHREADS_INHERIT_NONE); }
public function startWithClassLoader($option, $loader) { $this->loader = $loader; $this->loader->add('Wassa\\MPS\\', __DIR__ . '/../../../../../wassa/mobile-push-server/lib/'); parent::start($option); }
function cmdBackground($connection = 0, $sql = "", $silent = false, $values = array()) { $t = new Thread(array($this, "cmd")); $t->start($connection, $sql, true, $values); return true; }
<?php require_once 'php-thread-master/Thread.php'; // test to see if threading is available /*if( ! Thread::available() ) { die( 'Threads not supported' ); }*/ // function to be ran on separate threads function paralel($_limit, $_name) { for ($index = 0; $index < $_limit; $index++) { echo 'Ahora corriendo hilo ' . $_name . PHP_EOL; sleep(1); } } // create 2 thread objects $t1 = new Thread('paralel'); $t2 = new Thread('paralel'); // start them $t1->start(10, 't1'); $t2->start(10, 't2'); // keep the program running until the threads finish while ($t1->isAlive() && $t2->isAlive()) { sleep(1); }