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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?