Created
July 29, 2015 12:17
-
-
Save GreenRover/e8f2a4b4fb2a2e175175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Singelton extends ArrayObject { | |
private static $_registry = null; | |
public static function getInstance() { | |
if (self::$_registry === null) { | |
self::$_registry = new Singelton(); | |
} | |
return self::$_registry; | |
} | |
public static function get($index) { | |
return self::getInstance()->offsetGet($index); | |
} | |
public static function set($index, $value) { | |
self::getInstance()->offsetSet($index, $value); | |
} | |
} | |
// Fill values into singleton. | |
Singelton::set('test', 'grr'); | |
// Test singleton read in main thread. | |
echo 'Main Thread: ' . Singelton::get('test') . "\n"; | |
class workerThread extends Thread { | |
protected $i; | |
public function __construct($i) { | |
$this->i = $i; | |
} | |
public function run() { | |
// Read singleton in child thread. | |
echo 'a' . $this->i . ' ' . Singelton::get('test') . "\n"; | |
} | |
} | |
// Run max 10 paralel. | |
$pool = new Pool(10); | |
for ($i = 0; $i < 3; $i++) { | |
$pool->submit( | |
new workerThread($i) | |
); | |
} | |
$pool->shutdown(); | |
echo "MAIN finished\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment