PHP:在线程之间共享静态变量 [英] PHP: Sharing a static variable between threads

查看:93
本文介绍了PHP:在线程之间共享静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP 的不同线程之间共享静态变量时遇到问题.简单地说,我想1.在一个线程中写一个静态变量2.在其他线程中读取并执行所需的过程并清理它.为了测试上述要求,我写了下面的 PHP 脚本.

I have a problem in sharing static variable between different threads in PHP. In simple words I want to 1. Write a static variable in one thread 2. Read it in other thread and do the required process and clean it. For testing above requirement I have written below PHP script.

<?php

class ThreadDemo1 extends Thread
{
private $mode;  //to run 2 threads in different modes
private static $test;  //Static variable shared between threads

//Instance is created with different mode
function __construct($mode) {
    $this->mode = $mode;            
}

//Set the static variable using mode 'w'
function w_mode() {
   echo 'entered mode w_mode() funcion';
   echo "<br />";

   //Set shared variable to 0 from initial 100
   self::$test = 100;

   echo "Value of static variable : ".self::$test;
   echo "<br />";
   echo "<br />";

   //sleep for a while
   sleep(1);

}

//Read the staic vaiable set in mode 'W'
function r_mode() {
   echo 'entered mode r_mode() function';
   echo "<br />";

   //printing the staic variable set in W mode
   echo "Value of static variable : ".self::$test;
   echo "<br />";
   echo "<br />";

   //Sleep for a while
   sleep(2);

}

//Start the thread in different modes
public function run() {

//Print the mode for reference
echo "Mode in run() method: ".$this->mode;
echo "<br />";

    switch ($this->mode)
    {

    case 'W':
          $this->w_mode();
          break;

   case 'R':
         $this->r_mode();
         break;

  default:
        echo "Invalid option";        

        }      
    }
}


$trd1 = new ThreadDemo1('W');
$trd2 = new ThreadDemo1('R');
$trd3 = new ThreadDemo1('R');
$trd1->start();
$trd2->start();
$trd3->start();
?>

预期的输出是,run() 方法中的模式:W进入模式 w_mode() 函数静态变量的值:100

Expected output is, Mode in run() method: W entered mode w_mode() funcion Value of static variable : 100

run() 方法中的模式:R进入模式 r_mode() 函数静态变量的值:100

Mode in run() method: R entered mode r_mode() function Value of static variable : 100

run() 方法中的模式:R进入模式 r_mode() 函数静态变量的值:100

Mode in run() method: R entered mode r_mode() function Value of static variable : 100

但实际上我得到的输出是,run() 方法中的模式:W进入模式 w_mode() 函数静态变量的值:100

But actually I am getting the output as, Mode in run() method: W entered mode w_mode() funcion Value of static variable : 100

run() 方法中的模式:R进入模式 r_mode() 函数静态变量的值:

Mode in run() method: R entered mode r_mode() function Value of static variable :

run() 方法中的模式:R进入模式 r_mode() 函数静态变量的值:

Mode in run() method: R entered mode r_mode() function Value of static variable :

....真的不知道原因.请帮忙.

....Really unaware of the cause. Please help.

推荐答案

静态变量在上下文之间不共享,原因是静态变量有一个类入口作用域,而处理程序是用来管理对象作用域的.

Static variables are not shared among contexts, the reason is that static variables have a class entry scope, and handlers are for managing the object scope.

当一个新线程启动时,静态数据被复制(删除复杂的变量,如对象和资源).

When a new thread is started, statics are copied (removing complex variables, like objects and resources).

静态作用域可以被认为是一种线程本地存储.

The static scope can be thought of as a kind of thread local storage.

此外,如果成员不是静态的……从 pthreads 定义派生的类的所有成员都被认为是公共的.

In addition, where members are not static ... all members of a class derived from a pthreads definition are considered public.

我鼓励您阅读随 pthread 分发的示例,它们也可以在 github 上找到.

I encourage you to read the examples distributed with pthreads, they are available on github too.

这篇关于PHP:在线程之间共享静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆