为什么 PHP 中的一些线程数组操作似乎不起作用? [英] Why do some threaded array operations in PHP appear not to work?

查看:53
本文介绍了为什么 PHP 中的一些线程数组操作似乎不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 pthread PHP 扩展的线程类:

I have this thread class that utilises the pthread PHP extension:

class Task extends Thread
{
    protected $arr = array();

    public function run()
    {
        $this->arr[] = 1;
        $this->arr[] = 2;
        $this->arr[] = 3;
        var_dump($this->arr);
    }
}
$thread = new Task();
$thread->start();
$thread->join();

输出莫名其妙地显示了一个空数组.有人能简单解释一下原因吗?

The output inexplicably shows an empty array. Could anybody briefly explain why?

推荐答案

我有一个解决方案,但没有一个可靠的解释,所以非常欢迎更多的答案.

I have a solution but not a solid explanation, so more answers would be very welcome.

这是我的 Threaded 孩子(为简洁起见进行了修剪):

Here is my Threaded child (trimmed for the sake of brevity):

class ObjectConstructorThreaded extends Threaded
{
    protected $worker;
    protected $className;
    protected $parameters;
    protected $objectKey;

    public function __construct($className, $parameters)
    {
        $this->className = $className;
        $this->parameters = $parameters;
    }

    public function setWorker(\Worker $worker)
    {
        $this->worker = $worker;
    }

    protected function getWorker()
    {
        return $this->worker;
    }

    public function run()
    {
        $reflection = new \ReflectionClass($this->className);
        $instance = $reflection->newInstanceArgs($this->parameters);
        $this->objectKey = $this->getWorker()->notifyObject($instance);
    }

    public function getObjectKey()
    {
        return $this->objectKey;
    }
}

还有 Worker(再次修剪):

class ObjectServer extends Worker
{
    protected $count = 0;
    protected $objects = array();

    public function notifyObject($object)
    {
        $key = $this->generateHandle();

        /*
        // Weird, this does not add anything to the stack
        $this->objects[$key] = $object;

        // Try pushing - fail!
        $this->objects[] = $object;

        // This works fine? (but not very useful)
        $this->objects = array($key => $object);
        */

        // Try adding - also fine!
        $this->objects = $this->objects + array($key => $object);

        return $key;
    }
}

最后,开始讨论:

$thread = new ObjectServer();
$thread->start();
$threaded = new ObjectConstructorThreaded($className, $parameters);
$threaded->setWorker($this->worker);
$thread->stack($threaded);

从我当时写的纯正评论可以看出,尝试插入或推送到数组失败,但重写它(通过将其设置为固定值或旧值和新条目的合并)似乎工作.

As you can see from the unadulterated comments I wrote at the time, attempts to insert or push to the array failed, but rewriting it (by setting it to a fixed value or a merge of the old value and the new entry) seems to work.

因此,我将线程视为使非平凡类型(数组和对象)有效地不可变,并且它们只能被重置而不能被修改.我对可序列化类也有同样的经历.

I'm therefore regarding threading as making non-trivial types (arrays and objects) effectively immutable, and that they can only be reset and not modified. I've had the same experience with serialisable classes too.

至于为什么是这种情况,或者如果有更好的方法,如果我发现我会更新这个答案!

As to why this is the case, or if there is a better approach, I will update this answer if I find out!

这篇关于为什么 PHP 中的一些线程数组操作似乎不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆