PHP 8.3.4 Released!

Класс Thread

(PECL pthreads >= 2.0.0)

Введение

Когда вызван стартовый метод Thread, код метода run будет запущен в отдельном потоке, параллельно.

После отработки метода run, Thread сразу же завершит работу. Он будет присоединён путём создания Thread в нужное время.

Внимание

Если полагаться на движок для определения, когда Thread будет присоединён, можно столкнуться с неожиданным поведением. Поэтому необходимо, по возможности, управлять присоединением в явном виде.

Обзор классов

class Thread extends Threaded implements Countable, Traversable, ArrayAccess {
/* Методы */
public getCreatorId(): int
public static getCurrentThread(): Thread
public static getCurrentThreadId(): int
public getThreadId(): int
public isJoined(): bool
public isStarted(): bool
public join(): bool
public start(int $options = ?): bool
/* Наследуемые методы */
public Threaded::chunk(int $size, bool $preserve): array
public Threaded::extend(string $class): bool
public Threaded::merge(mixed $from, bool $overwrite = ?): bool
public Threaded::synchronized(Closure $block, mixed ...$args): mixed
public Threaded::wait(int $timeout = ?): bool
}

Содержание

add a note

User Contributed Notes 2 notes

up
6
german dot bernhardt at gmail dot com
7 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
2
german dot bernhardt at gmail dot com
9 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();
}

?>
To Top