Last active
May 18, 2016 08:43
-
-
Save ajcastro/7e0ac4003feeb7feaa12101c734006ac to your computer and use it in GitHub Desktop.
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 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