Ejemplo n.º 1
0
     */
    public function __construct($data)
    {
        $this->data = $data;
        $this->mine = strrev($this->data);
    }
    public function run()
    {
        printf("IN->%s: %s\n", __METHOD__, microtime(true));
        printf("%s: %s\n", __METHOD__, $this->synchronized(strrev($this->data)));
        printf("%s: %s\n", __METHOD__, microtime(true));
        printf("%s: %s\n", __METHOD__, $this->noaccess());
        printf("OUT->%s: %s\n", __METHOD__, microtime(true));
        return $this->data;
    }
}
/*
* This comment is not in use.
*/
$thread = new ExampleThread(rand() * 10);
$thread->start();
/*
* You can see that this call is blocked until the threading context returns from the method
*/
printf("Process: %s\n", $thread->synchronized());
/*
* Passing an argument on the command line will show you what happens when you call a private method from here
*/
if ($argv[1]) {
    printf("Process: %s\n", $thread->noaccess());
}
Ejemplo n.º 2
0
            echo "\n";
            /* always synchronize before calling notify/wait */
            $this->synchronized(function ($me) {
                /* there's no harm in notifying when no one is waiting */
                /* better that you notify no one than deadlock in any case */
                $me->notify();
            }, $this);
        } catch (EngineException $e) {
            var_dump($e);
        }
    }
}
/* construct the new thread */
$t = new ExampleThread();
/* start the new thread */
if ($t->start()) {
    printf("\nProcess Working ...\n");
    do_some_work(1000);
    /* synchronize in order to call wait */
    $t->synchronized(function ($me) {
        /*
         * note: do not stay synchronized for longer than you must
         *   this is to reduce contention for the lock 
         *   associated with the threads internal state
         */
        printf("\nProcess Waiting ...\n");
        $me->wait();
        printf("Process Done ...\n");
    }, $t);
}
echo "finished script...\n";