用pthreads连续更新数组

Im trying to make a pthreads class that continuously updates a variable that I'll be using elsewhere in the script. The pthreads class should pull the contents of a URL, assign the value to the variable, sleep and repeat.

$pair = array();

class Pair extends Thread {
    public function __construct($url){
         $this->url = $url;
    }

    public function run() {
        global $pair;
        while (true){
        $pair = json_decode("https://btc-e.com/api/2/".$this->url."/ticker", true);
        sleep(5);
        }
    }
}

$BTC_USD = new Pair("btc_usd");

while (true){
    print_r($pair);
    sleep(5);
}

$pair needs to be continuously updated and printed to the screen

<?php
define ('SECOND', 1000000);

class Repeater extends Thread {
    public $url;
    public $data;

    public function __construct($url) {
        $this->url = $url;
    }

    public function run() {
        do {
            /* synchronize the object */
            $this->synchronized(function(){
                /* fetch data into object scope */
                $this->data = file_get_contents($this->url);

                /* wait or quit */
                if (!$this->stop)
                    $this->wait();
            });
        } while (!$this->stop);
    }

    public function stop() {
        /* synchronize the object */
        $this->synchronized(function(){
            /* set stop */
            $this->stop = true;

            /* notify the object (could/should be waiting) */
            $this->notify();
        });
    }

    public function getData() {
        /* fetch data / wait for it to arrive */
        return $this->synchronized(function(){
            return $this->data;
        });
    }
}

/* to provide a limit for the example */
$iterations = 5;

/* the url etc is not important */
$repeat = new Repeater("http://www.google.com");
$repeat->start();

/* this will only return when there is data */
while (($data = $repeat->getData())) {
    var_dump(strlen($data));

    $repeat->synchronized(function($repeat){
        /* notify allows the thread to make he next request */
        $repeat->notify();
        /* waiting here for three seconds */
        $repeat->wait(3 * SECOND);
    }, $repeat);

    if (!--$iterations) {
        /* because examples should be sensible */
        $repeat->stop();
        break;
    }   
}
?>

It does not seem like a good use of resources to have a request per thread, you probably don't really want to do that in the real world. sleep is not suitable for use in multi-threading, it does not leave threads in a receptive state.

globals have no effect in the context of threading, they work as they usually do with regard to scope, but it is not inclusive of the scope of a thread.