PHP 8.1.0 Beta 1 available for testing

Thread クラス

(PECL pthreads >= 2.0.0)

はじめに

このオブジェクトの start メソッドが呼ばれると、run メソッドのコードが個別のスレッドで並列処理されます。

run メソッドの実行後は Thread はすぐに終了し、作成元のスレッドに適切な時期に join します。

警告

Thread をいつ join させるのかをエンジンに決めさせていると、予期せぬ振る舞いを引き起こすことがあります。 可能な限り、プログラマーが明示的に指定するようにしましょう。

クラス概要

Thread extends Threaded implements Countable , Traversable , ArrayAccess {
/* メソッド */
public detach(): void
public getCreatorId(): int
public static getCurrentThread(): Thread
public static getCurrentThreadId(): int
public getThreadId(): int
public static globally(): mixed
public isJoined(): bool
public isStarted(): bool
public join(): bool
public kill(): void
public start(int $options = ?): bool
/* 継承したメソッド */
public Threaded::chunk(int $size, bool $preserve): array
public Threaded::count(): int
public Threaded::extend(string $class): bool
public Threaded::from(Closure $run, Closure $construct = ?, array $args = ?): Threaded
public Threaded::isRunning(): bool
public Threaded::isTerminated(): bool
public Threaded::isWaiting(): bool
public Threaded::lock(): bool
public Threaded::merge(mixed $from, bool $overwrite = ?): bool
public Threaded::notify(): bool
public Threaded::notifyOne(): bool
public Threaded::pop(): bool
public Threaded::run(): void
public Threaded::synchronized(Closure $block, mixed ...$args): mixed
public Threaded::unlock(): bool
public Threaded::wait(int $timeout = ?): bool
}

目次

add a note add a note

User Contributed Notes 3 notes

up
27
german dot bernhardt at gmail dot com
7 years ago
<?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();
}

?>
up
3
german dot bernhardt at gmail dot com
5 years ago
<?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();

?>
up
-64
312036773 at qq dot com
5 years ago
<?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();
To Top