Source for file passing_vars.php
Documentation is available at passing_vars.php
* PHP_Fork class usage examples
* ==================================================================================
* NOTE: In real world you surely want to keep each class into
* a separate file, then include() it into your application.
* For this examples is more useful to keep all_code_into_one_file,
* so that each example shows a unique feature of the PHP_Fork framework.
* ==================================================================================
* This example shows variable exchange between the parent process
* and started pseudo-threads. This was not possible in previous releases because
* parent and child processes lives into different memory spaces,
* they are separate processes with their own PID and not separate instances living
* into the same JVM; this is just forking, not real threading...
* This framework offers a workaround based Shared Memory usage.
* ==================================================================================
require_once ("PHP/Fork.php");
// number of executeThreads we want
// Class definition; as into previos example (basic.php), this class simply
// increment a counter each second; instead of printing the value to stdout,
// we'll store it into an accessible location for latter use.
// setVariable is a method inherited from PHP_Fork super class
// it sets a variable that can be accessed thru its name
// by parent process (calling the getVariable() method)
$this->setVariable ('counter', $this->counter++ );
// parent process can call this facility method
// in order to get back the actual value of the counter
return $this->getVariable ('counter');
// Main program. Bring up NUM_THREAD instances of the executeThread class that
// runs concurrently. It's a multi-thread app with a few lines of code!!!
// Into this example we have a console to control thread behaviour and test
$executeThread[$i]->start ();
echo "Started " . $executeThread[$i]->getName () . " with PID " . $executeThread[$i]->getPid () . "...\n";
echo "This is the main process.\nPress [X] to terminate, [G] to read pseudo-thread's counter.\n";
// Console simple listener
$executeThread[$i]->stop ();
echo "Stopped " . $executeThread[$i]->getName () . "\n";
echo $executeThread[$i]->getName () . " returns " . $executeThread[$i]->getCounter() . "\n";
// Functions used by the console
$fp = fopen("php://stdin", "r");
$input = fgets($fp, 255 );
Documentation generated on Mon, 11 Mar 2019 15:41:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|