Skip to content

Instantly share code, notes, and snippets.

@hiend
Last active November 2, 2016 19:37
Show Gist options
  • Select an option

  • Save hiend/53c2a8e554957a85746f to your computer and use it in GitHub Desktop.

Select an option

Save hiend/53c2a8e554957a85746f to your computer and use it in GitHub Desktop.

Revisions

  1. hiend revised this gist May 3, 2014. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -29,13 +29,16 @@ public function run()
    }
    }

    $threads = array();
    $thread_count = 8;
    $start_time = microtime(true);

    for($i=1; $i<=$thread_count; $i++)
    while($thread_count--)
    {
    $thread[$i] = new hashThread(rand(10000, 100000), $i);
    $thread[$i]->start(PTHREADS_INHERIT_NONE);
    $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";
  2. hiend created this gist May 3, 2014.
    41 changes: 41 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <?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";
    }
    }

    $thread_count = 8;
    $start_time = microtime(true);

    for($i=1; $i<=$thread_count; $i++)
    {
    $thread[$i] = new hashThread(rand(10000, 100000), $i);
    $thread[$i]->start(PTHREADS_INHERIT_NONE);
    }

    echo "Done in: " . round(microtime(true) - $start_time, 2) . " seconds\r\n";