Skip to content

Instantly share code, notes, and snippets.

@rickynguyen4590
Created January 24, 2021 06:06
Show Gist options
  • Save rickynguyen4590/b5a71d91b2a36124e48b5d5d9d3f766e to your computer and use it in GitHub Desktop.
Save rickynguyen4590/b5a71d91b2a36124e48b5d5d9d3f766e to your computer and use it in GitHub Desktop.
Sample code pthreads 3.20 in php 7.2
<?php
class KikiThreaded extends Thread
{
private $id;
public function __construct($id)
{
$this->id = $id;
}
//This is where all of your work will be done.
public function run()
{
for ($i = 0; $i < 10; $i++) {
echo sprintf('..hi thread %s..', $this->id);
if ($this->id === 0) {
echo PHP_EOL;
}
sleep(rand(2, 10));
}
}
}
$allThreads = [];
for ($i = 0; $i < 5; $i++) {
$newThread = new KikiThreaded($i);
$newThread->start();
$allThreads[] = $newThread;
}
array_map(function ($thread) {
$thread->join();
}, $allThreads);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment