dqc19941228 2014-10-01 21:00
浏览 57
已采纳

为什么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?

  • 写回答

1条回答 默认 最新

  • dsb53973 2014-10-01 21:18
    关注

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

    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;
        }
    }
    

    And the Worker (again trimmed):

    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;
        }
    }
    

    Finally, to kick off the thread:

    $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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?