Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Last active May 27, 2020 07:25
Show Gist options
  • Select an option

  • Save PJZ9n/baebb82924faaeb6e9b13634fc2d12a2 to your computer and use it in GitHub Desktop.

Select an option

Save PJZ9n/baebb82924faaeb6e9b13634fc2d12a2 to your computer and use it in GitHub Desktop.
pthreadsを使ったマルチスレッディングのテスト
<?php
declare(strict_types=1);
class ExampleThread extends Thread
{
/** @var int */
private $id;
public function __construct(int $id)
{
$this->id = $id;
}
public function run()
{
echo "THREAD #{$this->id} PROCESS" . PHP_EOL;
sleep(1);
}
}
$pool = new Pool(5);
for ($i = 0; $i < 5; $i++) {
echo "THREAD #{$i} START" . PHP_EOL;
$pool->submit(new ExampleThread($i));
}
while ($pool->collect()) {
//
}
$pool->shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment