Some pits and points to pay attention to under pthreads v3

1. The child thread cannot access the global variables of the parent thread, but the parent thread can access the variables of the child thread

<?php

class Task extends Thread
{
    public $data;

    public function run()
    {
        global $num;
        var_dump ($ num);
        $this->data = 'abc';

        // this is printing null
        var_dump ($ GLOBALS);
        $ GLOBALS ['test'] = 'Def';
    }
}

//Global variables in the main thread cannot be accessed in child threads
//Create a child thread in php, it will have a separate heap and run in a separate address space
//Not like in some languages, child threads can access variables in the main thread.
$num = 666;

$GLOBALS['test'] = 'test';

$t = new Task();
$t->start() && $t->join();

//The main thread can access the variables of the child thread
var_dump($t->data);

The result is as follows:

 

2. The child thread cannot modify the variable of the parent thread

<?php

class Task extends Thread
{
    private $data;

    public function __construct(&$data)
    {
        $this->data = $data;
    }

    public function run()
    {
        echo "task data : ", $this->data, "\n";
        $this->data = 'def';
        echo "task data : ", $this->data, "\n";
    }
}

$data = 'abc';

//We pass in a reference here
$t = new Task($data);
$t->start() && $t->join();

//But the $data variable data has not changed
//This shows that the $data we passed in the Task object through the constructor is just a copy
//The child thread cannot operate the variables in the main thread
var_dump ($ data);

The result is as follows:

 

3. The pthreads v3 version can set members to anonymous functions

<?php

class Task extends Thread
{
    private $call;


    public function __construct()
    {
        //The pthreads v3 version seems to be able to set members to anonymous functions
        //It doesn't seem to be possible in the v2 version
        $this->call = function ($param1, $param2) {
            echo "task call param1 : {$param1} param2 : {$param2}\n";
        };
    }

    public function run()
    {
        //Call member anonymous function directly
        ($this->call)("hello", "world");
    }
}

$t = new Task();
$t->start() && $t->join();

The result is as follows:

 

Fourth, for the database connection resource, we need to declare it as a static member

<?php

class Task extends Thread
{
    private $db;

    public function __construct()
    {
        //Note that an error will be reported here, and the PDO instance cannot be serialized or deserialized
        $this->db = new PDO('mysql:dbname=test;host=192.168.33.226', 'root', '');
    }

    public function run()
    {
        $result = $this->db->query("select id,name from tb_user");
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            echo "{$row['id']}\t{$row['name']}\n";
        }
    }
}

$t = new Task();
$t->start() && $t->join();

The result is as follows:

The code is modified as follows:

<?php

class Task extends Thread
{
    //We need to declare the database connection resource as a static member, and then call the static method to create it
    private static $db;

    //We instantiate $db directly in the __construct() constructor, it seems to be null, you can try it if you are interested
    public static function getConn()
    {
        if (!is_resource(self::$db)) {
            self::$db = new PDO('mysql:dbname=test;host=192.168.33.226', 'root', '');
        }
        return self::$db;
    }

    public function run()
    {
        $result = self::getConn()->query("select id,name from tb_user");
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            echo "{$row['id']}\t{$row['name']}\n";
        }
    }
}

$t = new Task();
$t->start() && $t->join();

The result is as follows:

 

Finally, it is not ruled out that some of the above problems will not occur in the later version upgrades of pthreads.

My php version is 7.2.4 and pthreads version is 3.1.7dev

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324712462&siteId=291194637