PDA

View Full Version : Real Userland-Threads in PHP!



kung foo man
1st December 2012, 21:48
Hey all,

i know we have at least 2 PHP-Scripters here, so i am sharing this great finding with you.

On https://github.com/krakjoe/pthreads a british programmer added a git-archive, which contains everything we need. :)

We just need to compile it:




wget http://uk3.php.net/get/php-5.4.7.tar.bz2/from/this/mirror -O "php-5.4.7.tar.bz2"
tar -xf php-5.4.7.tar.bz2
cd php-5.4.7
cd ext
git clone https://github.com/krakjoe/pthreads.git
cd ../
./buildconf --force
./configure --disable-all --enable-pthreads --enable-maintainer-zts
make
TEST_PHP_EXECUTABLE=sapi/cli/php sapi/cli/php run-tests.php ext/pthreads


On my compile-test i just had to add autoconf:




apt-get update # für autoconf apt-get install autoconf

This is an example script:

thread.php


<?php
/*
* Method Scope 101
*
* protected:
* A protected method may only be called by one thread at a time
* If Thread A is calling a protected method and Thread B attempts to call the same method Thread B will block until Thread A has left the method
*
* private:
* private with respect to the scope of the Thread, ie: you can only call private methods from within the threading context
*/
class ExampleThread extends Thread {
private $mine = "mine";

/*
* This private method can only be called within the threading context
*/
private function noaccess(){
return sprintf("%lu: ran %s\n", $this->getThreadId(), __METHOD__);
}

/*
* This protected method can only be called by one thread at a time
*/
protected function synchronized($arg = null){
printf("IN->%s: %s\n", __METHOD__, microtime(true));
if ($arg)
$result = sprintf("%s: got \"%s\"", __METHOD__, $arg);
else $result = sprintf("%s: got nothing", __METHOD__);
printf("%s: %s\n", __METHOD__, microtime(true));
usleep(1000000);
printf("OUT->%s: %s\n", __METHOD__, microtime(true));
return $result;
}

/*
* Nothing special from here on in ...
*/
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));
}
}

/*
* 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());
?>


Have fun with PHP :)