Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP>;Pthreads检测到试图连接到已被销毁的对象_Php_Pthreads - Fatal编程技术网

PHP>;Pthreads检测到试图连接到已被销毁的对象

PHP>;Pthreads检测到试图连接到已被销毁的对象,php,pthreads,Php,Pthreads,如果我这样做: object(Volatile)#1 (1) { [0]=> string(3) "str" } TLDR快速解决方案: $this->v[] = (array) ["array" => ["test"]]; 键入cast以在内存中对数组进行不同的初始化 <?php $v = new Volatile; $t = new class($v) extends Thread { function __construct(Volatile $

如果我这样做:

object(Volatile)#1 (1) {
  [0]=>
  string(3) "str"
}

TLDR快速解决方案:

$this->v[] = (array) ["array" => ["test"]];
键入cast以在内存中对数组进行不同的初始化

<?php
$v = new Volatile;

$t = new class($v) extends Thread
{
   function __construct(Volatile $v)
   {
      $this->v = $v;
   }

   function run()
   {
      $this->v[] = ["array" => ["test"]];
   }
};

$t->start(); $t->join();

var_dump($v);
工作示例:

$this->v[] = (array) ["array" => ["test"]];
解释:

$this->v[] = (array) ["array" => ["test"]];
您的问题是由于生成的线程和主程序之间难以共享内存

新线程使用自己的“私有”内存分配运行,远离主程序。父线程无法访问线程使用的内存。当线程结束时,垃圾收集器会清理垃圾;销毁易挥发物体;因此,刚才设置的数据不再存在,因此出现错误)

那么,为什么您能够用字符串而不是数组来写它呢?因为PHP在幕后初始化不同变量类型的方式!我怀疑初始化字符串(或整数)是由PHP在主堆中完成的,因此可以全局访问。数组不同;它们是更复杂的引用类型,初始化方式不同。如果您先初始化整数,那么“str”语句也可以处理整数(见下文)!在线程内部,数组作为易失性对象处理

虽然在语法上相似,但这两条语句以不同的方式初始化和寻址内存:

class My extends Thread {
    public function run() {
        $arr = array(
            [ "foo" => "bar" ],
            [ "someInt" => 3 ],
            7       => "seven",
            "dog"   => "jack russell"           
        );
        $this->result = $arr;            // Result is Volatile object 
        $this->result = (array) $arr;    // Result is Array()
    }
}
$th = new My(); $th->start(); $th->join();
var_dump($my->result); // Array, works
然而:

与“str”示例类似,以下整数类型也适用:

// What we are saying here is:
// $this->[] Should be initialized as "str" (possibly in the main heap)
$this->v[] = "str"; // *contents* of $this->v[] available to both threads and remains in memory after the thread completes

您想要的是为作业/线程指定内存地址,它应该将其结果转储为数组类,而不是Volatile类。(而不是试图在线程内创建一个内存地址,并将指向该地址的指针返回到父线程,这当然不会起作用,因为当线程完成时,该内存的内容将被擦除)

TLDR快速解决方案:

$this->v[] = (array) ["array" => ["test"]];
键入cast以在内存中对数组进行不同的初始化

<?php
$v = new Volatile;

$t = new class($v) extends Thread
{
   function __construct(Volatile $v)
   {
      $this->v = $v;
   }

   function run()
   {
      $this->v[] = ["array" => ["test"]];
   }
};

$t->start(); $t->join();

var_dump($v);
工作示例:

$this->v[] = (array) ["array" => ["test"]];
解释:

$this->v[] = (array) ["array" => ["test"]];
您的问题是由于生成的线程和主程序之间难以共享内存

新线程使用自己的“私有”内存分配运行,远离主程序。父线程无法访问线程使用的内存。当线程结束时,垃圾收集器会清理垃圾;销毁易挥发物体;因此,刚才设置的数据不再存在,因此出现错误)

那么,为什么您能够用字符串而不是数组来写它呢?因为PHP在幕后初始化不同变量类型的方式!我怀疑初始化字符串(或整数)是由PHP在主堆中完成的,因此可以全局访问。数组不同;它们是更复杂的引用类型,初始化方式不同。如果您先初始化整数,那么“str”语句也可以处理整数(见下文)!在线程内部,数组作为易失性对象处理

虽然在语法上相似,但这两条语句以不同的方式初始化和寻址内存:

class My extends Thread {
    public function run() {
        $arr = array(
            [ "foo" => "bar" ],
            [ "someInt" => 3 ],
            7       => "seven",
            "dog"   => "jack russell"           
        );
        $this->result = $arr;            // Result is Volatile object 
        $this->result = (array) $arr;    // Result is Array()
    }
}
$th = new My(); $th->start(); $th->join();
var_dump($my->result); // Array, works
然而:

与“str”示例类似,以下整数类型也适用:

// What we are saying here is:
// $this->[] Should be initialized as "str" (possibly in the main heap)
$this->v[] = "str"; // *contents* of $this->v[] available to both threads and remains in memory after the thread completes
您想要的是为作业/线程指定内存地址,它应该将其结果转储为数组类,而不是Volatile类。(而不是试图在线程内创建内存地址,并将指向该地址的指针返回到父线程,这当然永远不会起作用,因为当线程完成时,该内存的内容将被擦除)