dsfdsf8888 2016-02-13 12:46
浏览 48
已采纳

关联数组作为PHP中的对象属性(使用pthreads)

I have a associative array type property in my object. Here's the example:

class GetInfo {    

    private $domains_ip = array();

    function get_ip($domain)
    {

        print_r($this->domains_ip);
        $ip = gethostbyname($domain);       
        $this->domains_ip[$ip] = $domain;       
        return $ip;      

    }

}

class my_thread extends Thread {

    private $get_info_object;

    function __construct(GetInfo $obj)
    {
        $this->get_info_object = $obj;
    }

    function check_ip($domain)
    {
        echo $this->get_info_object->get_ip($domain);
    }

}
$o = new GetInfo();
$t = new my_thread($o);

$t->check_ip("google.com");
$t->check_ip("pivo.com");

But problem is that $this->domains_ip value doesn't change. What appropriate construction should I use in order to add value in this type of property. It works fine without passing it to thread object, but I it needed for my task. Thank you.

  • 写回答

1条回答 默认 最新

  • dpkpaxhzffixp8426 2016-02-13 13:15
    关注

    Since GetInfo does not descend from pthreads (it is not Threaded):

    $this->get_info_object = $obj;
    

    This results in a serial representation of $obj being stored as a member of the Thread. This results in the members of GetInfo being serialized and will have unexpected results.

    The solution is to replace your usage of arrays with suitable objects, the following code is for PHP 7 (pthreads v3+):

    <?php
    class GetHostByNameCache extends Threaded {
    
        public function lookup(string $host) : string {
            return $this->synchronized(function() use($host) {
                if (isset($this->cache[$host])) {
                    return $this->cache[$host];
                }
    
                return $this->cache[$host] = gethostbyname($host);
            });
        }
    
        private $cache = [];
    }
    
    class Test extends Thread {
    
        public function __construct(GetHostByNameCache $cache, string $host) {
            $this->cache = $cache;
            $this->host = $host;
        }
    
        public function run() {
            var_dump(
                $this->cache->lookup($this->host));
        }
    
        private $cache;
    }
    
    $tests = [];
    $cache = new GetHostByNameCache();
    $domains = [
        "google.co.uk",
        "google.com",
        "google.co.jp",
        "google.co.in",
        "google.co.ca",
        "google.net"
    ];
    
    for ($test = 0; $test < count($domains); $test++) {
        $tests[$test] = new Test($cache, $domains[$test]);
        $tests[$test]->start();
    }
    
    foreach ($tests as $test)
        $test->join();
    
    var_dump($cache);
    ?>
    

    This will yield something like:

    string(14) "216.58.198.195"
    string(14) "216.58.198.206"
    string(14) "216.58.198.195"
    string(14) "216.58.198.195"
    string(12) "66.196.36.16"
    string(14) "216.58.198.196"
    object(GetHostByNameCache)#1 (1) {
      ["cache"]=>
      object(Volatile)#2 (6) {
        ["google.co.uk"]=>
        string(14) "216.58.198.195"
        ["google.com"]=>
        string(14) "216.58.198.206"
        ["google.co.jp"]=>
        string(14) "216.58.198.195"
        ["google.co.in"]=>
        string(14) "216.58.198.195"
        ["google.co.ca"]=>
        string(12) "66.196.36.16"
        ["google.net"]=>
        string(14) "216.58.198.196"
      }
    }
    

    The important things to notice are:

    • The cache object is Threaded.
    • The array that appears to be used in the cache is cast to Volatile.
    • The lookup routine logic is synchronized.

    Because lookup is synchronized, not more than one thread can perform a lookup at a time, this ensures no two threads can perform the same lookup twice. You might be able to come up with a more efficient way of synchronizing access to the cache (on a per record basis), but this is a good starting place.

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

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?