Last active
November 2, 2016 19:37
-
-
Save hiend/53c2a8e554957a85746f 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 hashThread extends Thread { | |
private $max = 1; | |
private $index = 0; | |
function __construct($max, $index) | |
{ | |
$this->max = $max; | |
$this->index = $index; | |
} | |
public function run() | |
{ | |
if (in_array($this->index, [2,4,6,8])) { | |
require 'null'; | |
} | |
if (in_array($this->index, [7])) { | |
throw Exception("stop"); | |
} | |
for ($i=1; $i<=$this->max; $i++) | |
{ | |
md5($i); | |
} | |
echo "Thread #{$this->index} finished\r\n"; | |
} | |
} | |
$threads = array(); | |
$thread_count = 8; | |
$start_time = microtime(true); | |
while($thread_count--) | |
{ | |
$threads[$thread_count] = new hashThread(rand(10000, 100000), $thread_count); | |
$threads[$thread_count]->start(PTHREADS_INHERIT_NONE); | |
} | |
array_map(function($thread) { $thread->join(); }, $threads); | |
echo "Done in: " . round(microtime(true) - $start_time, 2) . " seconds\r\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment