Example #1
0
 function singleThreadLoop()
 {
     $thread = new DummyThread();
     while (true) {
         while (!($task = $this->TaskModel->getNextTask())) {
             // have a sleep for a second until we get more tasks (hopefully)
             sleep(1);
         }
         $id = $task['id'];
         $taskName = $task['task'];
         $methodName = $task['method'];
         $data = $task['data'];
         $thread->setVariable('taskId', $id);
         $thread->setVariable('task', $taskName);
         $thread->setVariable('method', $methodName);
         $thread->setVariable('data', $data);
         $thread->setVariable('status', PseudoThread::BUSY);
         $this->TaskModel->taskExecuting($id);
         if (!$this->loadTask($taskName)) {
             continue;
         }
         $taskClass =& $this->{$taskName};
         if (empty($methodName)) {
             $methodName = 'execute';
         }
         $taskClass->thread =& $thread;
         $taskClass->pid = 1;
         // just a single thread, so we just give a fake process id
         $taskClass->data =& $data;
         $taskClass->{$methodName}();
         if ($thread->getVariable('status') === PseudoThread::ERROR) {
             $this->TaskModel->taskError($id);
         } else {
             $this->TaskModel->taskComplete($id);
         }
     }
 }
Example #2
0
require __DIR__ . "/../vendor/autoload.php";
use AppserverIo\Fhreads\Thread;
class DummyThread extends Thread
{
    public function run()
    {
        $this->synchronized(function ($self) {
            echo 'In Synchronized Thread Context.' . PHP_EOL;
            $self->b = new stdClass();
            sleep(1);
        });
        usleep(100);
        $this->synchronized(function ($self) {
            echo "finished Thread run\n";
        });
    }
}
$objs1 = new stdClass();
$objs1 = new stdClass();
$objs1 = new stdClass();
$t = new DummyThread();
$t->start();
usleep(1000);
$t->synchronized(function ($self) use($objs1) {
    echo 'In Synchronized Global Context.' . PHP_EOL;
    $self->a = $objs1;
    sleep(1);
});
$t->join();
var_dump($t);
echo "finished script\n";