码迷,mamicode.com
首页 > 其他好文 > 详细

pthread小测试

时间:2016-03-20 14:26:47      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:

直接贴出来,来自PHP-Mannular下的note。

<?php
class My extends Thread
{
    public
        $array = array(‘default val 1‘, ‘default val 2‘),
        $msg = ‘default‘,
        $stop = false;

    public function run()
    {
        while(true)
        {
            echo $this->msg . PHP_EOL;
            if(count($this->array) > 0){
                foreach($this->array as $val){
                    var_dump($val);
                }
                $this->array = array();
            }
            /** cause this thread to wait **/
            $this->synchronized(
                function($thread){
                    if(count($this->array) < 1){
                        $thread->wait();
                    }
                },
                $this
            );
            echo PHP_EOL;
            if($this->stop){
                break;
            }
        } // while
    }
}
$my = new My();
$my->start();

sleep(1); // wait a bit

// test 1 - $thread->array[] = 1;
$my->synchronized(
    function($thread){
        $thread->msg = ‘test 1‘;
        $thread->array[] = 1;
        $thread->notify();
    },
    $my
);

sleep(1); // wait a bit

// test 2 - array_push($thread->array, 2);
$my->synchronized(
    function($thread){
        $thread->msg = ‘test 2‘;
        array_push($thread->array, 2);
        $thread->notify();
    },
    $my
);

sleep(1); // wait a bit

// test 3 - array_merge($thread->array, 3);
$my->synchronized(
    function($thread){
        $thread->msg = ‘test 3‘;
        $new = array(3);
        $thread->array = array_merge($thread->array, $new);
        $thread->notify();
    },
    $my
);

sleep(1); // wait a bit

$my->stop = true;
?>
out:
default
string(13) "default val 1"
string(13) "default val 2"

test 1

test 2

test 3
int(3)

so in this case only array_merge() worked.

可以看到当时他对thread数组属性改变操作中,只有array_merge可以实现。不过这是一年前的例子,我用php7.0 + php_pthreads-3.1.6-7.0运行后,会报错误,连array_merge也无法实现。

PHP Fatal error: Uncaught RuntimeException: Threaded members previously set to Threaded objects are immutable, cannot overwrite array in D:\Apache24\htdocs\threadtest.php:166

PHP Warning: array_push() expects parameter 1 to be array, object given in D:\Apache24\htdocs\threadtest.php on line 206

PHP Warning: array_merge(): Argument #1 is not an array in D:\Apache24\htdocs\threadtest.php on line 219

我猜想应该是thread会自动将数组属性转换成obj类型,并且会获得一些继承的方法,因此无法用常规的处理array的函数对其进行操作,但比如 Threaded::pop,Threaded::shift,等都是可以使用的。

就像这样:

<?php
class test extends thread{
public $arr = array(1,2,3,4); public function run() {
var_dump($this->arr->pop()); var_dump($this->arr->shift()); var_dump($this->arr); //常规方法 var_dump(array_pop($this->arr)); var_dump(array_shift($this->arr)); } } $a = new test(); $a->start(); ?> 输出结果: int(4) int(1) object(Volatile)#2 (2) { [1]=> int(2) [2]=> int(3) } PHP Warning: array_pop() expects parameter 1 to be array, object given in D:\Apache24\htdocs\threadtest.php on line 161 NULL PHP Warning: array_shift() expects parameter 1 to be array, object given in D:\Apache24\htdocs\threadtest.php on line 162 NULL

 

pthread小测试

标签:

原文地址:http://www.cnblogs.com/onyourway/p/5297586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!