(PECL pthreads >= 2.0.0)
Введение
Когда вызывается метод Thread::start , код метода run будет выполняться в отдельном потоке параллельно.
После выполнения метода run поток немедленно завершает работу, он присоединяется к соответствующему потоку в соответствующее время.
Внимание
Полагаясь на движок, чтобы определить, когда поток должен присоединиться, может вызвать нежелательное поведение; программист должен быть явным, где это возможно.
Обзор классов
class Thread extends Threaded implements Countable , Traversable , ArrayAccess {
/* Методы */
public void detach ( void )
public int getCreatorId ( void )
public static Thread getCurrentThread ( void )
public static int getCurrentThreadId ( void )
public int getThreadId ( void )
public static mixed globally ( void )
public bool isJoined ( void )
public bool isStarted ( void )
public bool join ( void )
public void kill ( void )
public bool start ([ int $options ] )
/* Наследуемые методы */
public array Threaded::chunk ( int $size , bool $preserve )
public int Threaded::count ( void )
public bool Threaded::extend ( string $class )
public Threaded Threaded::from ( Closure $run [, Closure $construct [, array $args ]] )
public array Threaded::getTerminationInfo ( void )
public bool Threaded::isRunning ( void )
public bool Threaded::isTerminated ( void )
public bool Threaded::isWaiting ( void )
public bool Threaded::lock ( void )
public bool Threaded::merge ( mixed $from [, bool $overwrite ] )
public bool Threaded::notify ( void )
public bool Threaded::notifyOne ( void )
public bool Threaded::pop ( void )
public void Threaded::run ( void )
public mixed Threaded::shift ( void )
public mixed Threaded::synchronized ( Closure $block [, mixed $... ] )
public bool Threaded::unlock ( void )
public bool Threaded::wait ([ int $timeout ] )
}
<?php
class workerThread extends Thread {
public function __construct($i){
$this->i=$i;
}
public function run(){
while(true){
echo $this->i;
sleep(1);
}
}
}
for($i=0;$i<50;$i++){
$workers[$i]=new workerThread($i);
$workers[$i]->start();
}
?>
<?php
# ERROR GLOBAL VARIABLES IMPORT
$tester=true;
function tester(){
global $tester;
var_dump($tester);
}
tester(); // PRINT -> bool(true)
class test extends Thread{
public function run(){
global $tester;
tester(); // PRINT -> NULL
}
}
$workers=new test();
$workers->start();
?>
<?php
class STD extends Thread{
public function put(){
$this->synchronized(function(){
for($i=0;$i<7;$i++){
printf("%d\n",$i);
$this->notify();
if($i < 6)
$this->wait();
else
exit();
sleep(1);
}
});
}
public function flush(){
$this->synchronized(function(){
for($i=0;$i<7;$i++){
flush();
$this->notify();
if($i < 6)
$this->wait();
else
exit();
}
});
}
}
class A extends Thread{
private $std;
public function __construct($std){
$this->std = $std;
}
public function run(){
$this->std->put();
}
}
class B extends Thread{
private $std;
public function __construct($std){
$this->std = $std;
}
public function run(){
$this->std->flush();
}
}
ob_end_clean();
echo str_repeat(" ", 1024);
$std = new STD();
$ta = new A($std);
$tb = new B($std);
$ta->start();
$tb->start();