Exemplo n.º 1
0
    {
        $this->mysql = $mysql;
        $this->mutex = $mutex;
    }
    public function run()
    {
        if ($this->mutex) {
            printf("LOCK(%d): %d\n", $this->getThreadId(), Mutex::lock($this->mutex));
        }
        if ($result = mysql_query("SHOW PROCESSLIST;", $this->mysql)) {
            while ($row = mysql_fetch_assoc($result)) {
                print_r($row);
            }
        }
        if ($this->mutex) {
            printf("UNLOCK(%d): %d\n", $this->getThreadId(), Mutex::unlock($this->mutex));
        }
    }
}
$mysql = mysql_connect("127.0.0.1", "root", "");
if ($mysql) {
    $mutex = Mutex::create();
    $instances = array(new MyShared($mysql, $mutex), new MyShared($mysql, $mutex), new MyShared($mysql, $mutex));
    foreach ($instances as $instance) {
        $instance->start();
    }
    foreach ($instances as $instance) {
        $instance->join();
    }
    Mutex::destroy($mutex);
}
Exemplo n.º 2
0
        $stdout = fopen("php://stdout", "w");
        while (++$i < rand(200, 400)) {
            echo ".";
            fflush($stdout);
        }
        echo "\n";
        fflush($stdout);
        /* and unlock mutex, making it ready for destruction */
        printf("Returning(%d) %f\n", Mutex::unlock($this->lock), microtime(true));
        fflush($stdout);
        /* you should close resources, or not, whatever; I'm not your mother ... */
        return null;
    }
}
/* create a mutex and acquire lock */
$lock = Mutex::create(true);
/* create new thread, passing the lock as parameter */
$thread = new Syncrhonizing($lock);
/* start executing the child ... */
$thread->start();
/* the child will block until you release the mutex */
printf("Allow(%d):  %f\n", Mutex::unlock($lock), microtime(true));
/* ... continue normal not thread related stuff ... */
/* and then: */
$thread->join();
/* 
	another option would be to lock the mutex and unlock it and let the thread destroy itgetThreadId without calling join,
	when this is stable that will definitely be an option but right now I suggest you join everything you create somewhere
*/
printf("Cleaup(%d):  %f\n", Mutex::destroy($lock), microtime(true));