So I have a main class that calls another singleton class but when running multiple threads (or concurrent threads) I get cross data contamination. This is a very simple version to explain the problem. All the variable setter/getters are in the Singleton and are called and set by the main class.
class A {
public function doSomething($var) {
Singleton::instance()->setVar($var);
}
public function showSomething() {
return Singleton::instance()->getVar();
}
}
// Singleton
class Singleton {
private static $instance = null;
private $var;
public static function instance() {
if(!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
public function setVar($var) {
$this->var = $var;
}
public function getVar() {
return $this->var;
}
}
test script 1:
$actions = Array(
'one',
'two',
'three',
);
foreach($actions as $act) {
$action = new A();
$action->doSomething($act);
echo "Action: ".$action->showSomething()."\n";
sleep(2);
}
test script 1 output will have;
one
two
three
test script 2:
$actions = Array(
'1',
'2',
'3',
);
foreach($actions as $act) {
$action = new A();
$action->doSomething($act);
echo "Action: ".$action->showSomething()."\n";
sleep(2);
}
test script 2 output will have;
1
2
3
one
two
three
(not in this order and it might be missing one of the values)
So why is test 1 included in test 2 results when executing both scripts at the same time?
How I’m testing:
open two terminals, execute one script in each terminal (hence the sleep) so I could see the data contamination.
You write in your question that test1 and test2 are two separate processes. From the example code you give I can not see that both scripts exchange data (not code) in a way that allows to be cross-process exchanged (e.g. via a file, session whatever).
However from the symptoms it looks like that when you execute test2, test1 is executed as well.
To better understand what’s going on, you can add code into your singleton class that echo’es outwhat it currently does. E.g. setting a variable or showing it. Then you can trace why your problem occurs. You should pretty fast find out where the problem has it’s root.
Alternatively if you have a IDE with debugger support at hand, you can do this by stepping through the execution as well.