douzhonglong3789 2015-05-16 16:24
浏览 129
已采纳

我如何在php多线程中使用静态方法

i got some problem with php multi thread,here is my code

<?php
class testClass {
    const TEST           = 'UTF-8';
    public static $test1 = array(
    'key' => 'value',
    );
    public static function test2() {
        return "this is static func";
    }
}
class My extends Thread {
    public static $test3;
    public static $test4 = "this is test4 string";
    public function __construct() {
        echo "main start
";
        self::$test3 = new DateTime();
        var_dump(testClass::TEST);
        var_dump(testClass::$test1);
        var_dump(testClass::test2());
        var_dump(self::$test3);
        var_dump(self::$test4);
        echo "main end



";
    }
    public function run() {
        echo "sub start
";
        var_dump(testClass::TEST);
        var_dump(testClass::$test1);
        var_dump(testClass::test2());
        var_dump(self::$test3);
        var_dump(self::$test4);
        echo "sub end

";
    }
}
$my = new My();
$my->start();
?>

and the result

#main start
string(5) "UTF-8"
array(1) {
  ["key"]=>
  string(5) "value"
}
string(19) "this is static func"
object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2015-05-16 23:26:19.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(3) "PRC"
}
string(20) "this is test4 string"
#main end

#sub start
string(5) "UTF-8"
NULL
string(19) "this is static func"
NULL
string(20) "this is test4 string"
#sub end

in this result , you can see the static String variable can get the value from the sub Thread , but the DateTime object is not

sub Thread call testClass::TEST can get the value, but call testClass::$test1 is not

my PHP Version => 5.6.8 configure '--enable-maintainer-zts'

I am at my wits end, any help at all is greatly appreciated

  • 写回答

1条回答 默认 最新

  • douzhan8652 2015-07-02 20:57
    关注

    From the pthreads introduction:

    Static Members: When a new context is created ( Thread or Worker ), they are generally copied, but resources and objects with internal state are nullified (for safety reasons). This allows them to function as a kind of thread local storage. For example, upon starting the context, a class whose static members include connection information for a database server, and the connection itself, will only have the simple connection information copied, not the connection. Allowing the new context to initiate a connection in the same way as the context that created it, storing the connection in the same place without affecting the original context.

    So you have to create your static objects/resources from scratch in each thread.

    Note: This applies to arrays as well.

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

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题