krakjoe / pthreads

Threading for PHP - Share Nothing, Do Everything :)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Child thread stuck in the function until other child threads are finished

DrOctavius opened this issue · comments

------------Pthreads Version:------------
zts-php -v
PHP 5.6.36 (cli) (built: Apr 25 2018 10:13:19)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

-------------Problem----------------
I have:

  1. The main process that launches 1 thread (class \Worker)
  2. This Worker that was launched by the main Thread launches afterall 5 more Threads (class \Thread, also tried with \Worker)

The problem is: when the Child Worker/Thread(from main process) launches 5 more threads, they actually work, they work fine, but this child is stuck in the function where i am creating these 5 threads, and it doesn't go futher until they are finished...

My question: why so? Why it's not going further by continuing it's further functionality... (it's blocked in the "createThreads" function...)

The ideea is to have child threads (even with infinite loops) and continue basic functionality...

The code it's much larger, but i'll give an example what i did:

  1. Save the code somewhere: testzts.php
  2. zts-php -f testzts.php
<?php


function debug($msg)
{
    $now = date('Y-m-d H:i:s');
    echo "[{$now}]: {$msg}\n";
}

// Main Process start First Thread!
$first_thread = new FirstThread();
$first_thread->start();
while (1) {
    debug('main thread sleep');
    sleep(1);
}

class FirstThread extends \Worker
{
    protected $threads_pool = [];
    protected $test_1_child;
    protected $test_2_child;

    const nr_of_threads = 5;

    public function __construct()
    {

    }

    public function run()
    {
        debug("First thread created!");
        // Create the Threads!
        $this->createThreads();
        while (1) {
            echo 'FIRST THREAD SLEEEEEPING';
            usleep(300000);
        }

    }

    protected function createThreads()
    {
//        for ($i = 1; $i <= self::nr_of_threads; $i++) {
//            $rand_id = time() . rand(500000, 9999999);
//            $this->threads_pool[$rand_id] = new SecondThread();
//            $this->threads_pool[$rand_id]->start();
//
//        }
      $this->test_1_child = new SecondThread();
        $this->test_1_child->start();
        $this->test_2_child = new SecondThread();
        $this->test_2_child->start();
        // // it DOESN'T GO OUT FROM HERE....
        debug("Pam param...");

    }
}

class SecondThread extends \Worker
{
    public function __construct()
    {

    }

    public function run()
    {

        while (1) {
            debug('hello from second thread');
            usleep(300000);
        }

    }
}

?>