示例#1
0
<?php

/*
* Note: in 5.4 series the output is handled differently from multiple threads on the command line, sometimes giving the idea that this sort of thing is executing the opposite to how it should...
* It isn't, but there's not much to be done about the new output handling in 5.4 ... we'll just have to live with it ...
*/
class ExampleThread extends Thread
{
    public function run()
    {
        printf("I'm in the thread and you're waiting for me ...\n");
        printf("Allowing process to continue ...\n");
        printf("Thread Notifying Process 1: %d\n", $this->notify());
        printf("Process Waiting ...\n");
        printf("Thread Notifying Process 2: %b\n", $this->notify());
        $this->wait();
        printf("Thread exiting ...");
    }
}
$t = new ExampleThread();
if ($t->start(true)) {
    printf("Process Working  ... then ...\n");
    printf("Process Waited: %d\n", $t->wait());
    printf("Now doing some work while thread is waiting ...\n");
    usleep(1000000);
    printf("And notify thread: %d\n", $t->notify());
}
示例#2
0
        $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->exclusive(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));
    }
}
/*
* This comment is not in use.
*/
$thread = new ExampleThread(rand() * 10);
echo 'Block 1 ' . PHP_EOL;
$thread->start();
echo 'Block 2 ' . PHP_EOL;
/*
* You can see that this call is blocked until the threading context returns from the method
*/
printf("Process: %s\n", $thread->exclusive());
echo 'Block 3 ' . PHP_EOL;
/*
* Passing an argument on the command line will show you what happens when you call a private method from here
*/
if (isset($argv[1]) && $argv[1]) {
    printf("Process: %s\n", $thread->noaccess());
}
echo 'Block 4 ' . PHP_EOL;
示例#3
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";