<?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); }
<?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";