start() public method

Will start a new Thread to execute the implemented run method
public start ( integer $options = PTHREADS_INHERIT_ALL ) : boolean
$options integer An optional mask of inheritance constants, by default PTHREADS_INHERIT_ALL
return boolean A boolean indication of success
Beispiel #1
0
 /**
  * @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());
 }
Beispiel #3
0
 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;
 }
Beispiel #4
0
 /**
  *	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();
 }
Beispiel #5
0
 function run()
 {
     while (true != $this->exit) {
         $sock = socket_accept($this->ls);
         if ($sock) {
             $hs = new Thread(new HttpHandler($sock, $this->handler));
             $hs->start();
         }
     }
 }
Beispiel #6
0
 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();
     }
 }
Beispiel #7
0
 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;
 }
Beispiel #8
0
 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);
     });
 }
Beispiel #9
0
 /**
  * {@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);
     }
 }
Beispiel #10
0
<?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";
Beispiel #11
0
 /**
  * Start the thread
  *
  * @throws \RuntimeException
  */
 public function execute()
 {
     $this->args = func_get_args();
     return parent::start();
 }
Beispiel #12
0
 public final function start($options = PTHREADS_INHERIT_ALL)
 {
     ThreadManager::getInstance()->add($this);
     return parent::start($options);
 }
Beispiel #13
0
<?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));
Beispiel #14
0
 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);
 }
Beispiel #16
0
 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;
 }
Beispiel #18
0
<?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);
}