Skip to content

Instantly share code, notes, and snippets.

@ajcastro
Last active May 18, 2016 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajcastro/7e0ac4003feeb7feaa12101c734006ac to your computer and use it in GitHub Desktop.
Save ajcastro/7e0ac4003feeb7feaa12101c734006ac to your computer and use it in GitHub Desktop.
<?php
class PThread extends Thread
{
protected $id = ''; //ThreadID
public function __construct($idThread)
{
$this->id = $idThread;
}
public function run()
{
if ($this->id) {
$sleep = mt_rand(1, 5);
printf('Thread: %s has started, sleeping for %d'."\n", $this->id, $sleep);
sleep($sleep);
printf('Thread: %s has finished'."\n", $this->id);
}
}
}
// Creating the pool of threads(stored as array)
$poolArr = array();
//Initiating the threads
foreach (range(0, 3) as $i) {
$poolArr[] = new PThread($i);
}
//Start each Thread
foreach ($poolArr as $t) {
$t->start();
}
//Wait all thread to finish
foreach (range(0, 3) as $i) {
$poolArr[$i]->join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment