Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Sharing a static variable between threads

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.

like image 931
user2515938 Avatar asked Oct 04 '22 18:10

user2515938


1 Answers

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.

like image 76
Joe Watkins Avatar answered Oct 07 '22 19:10

Joe Watkins