PHP: Sharing a static variable between threads

。_饼干妹妹 提交于 2019-12-22 09:31:42

问题


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();
?>

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

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

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

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

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

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

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


回答1:


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.

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

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




回答2:


How are you accomplishing multi-threading?

PHP doesn't have the same threading support as languages like Java, where you have a JVM which is constantly running at application level.

With PHP each page request is creating a new instance of PHP to handle that request, and the scope of the static variables are only for each running instance.

To share data between threads, you'll need to store value in the DB, session, or a simple file depending on your requirements.



来源:https://stackoverflow.com/questions/17274011/php-sharing-a-static-variable-between-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!