Skip to content

Instantly share code, notes, and snippets.

@hiend
Last active November 2, 2016 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hiend/53c2a8e554957a85746f to your computer and use it in GitHub Desktop.
Save hiend/53c2a8e554957a85746f to your computer and use it in GitHub Desktop.
<?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