Created
January 24, 2021 06:06
-
-
Save rickynguyen4590/b5a71d91b2a36124e48b5d5d9d3f766e to your computer and use it in GitHub Desktop.
Sample code pthreads 3.20 in php 7.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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