Class Thread

Description

Represents a thread running concurrent to other threads.

PHP doesn't support threads (exception). In most cases threads are no good idea. But they can be useful while working with external resources like network connections.

All your threads will run on one CPU. These are virtual threads.

You can directly download the source code:

How To Use

Create two threads and let them run concurrently:

  1.   class NetworkThread extends Thread {
  2.  
  3.       private $handle;
  4.       private $response '';
  5.  
  6.       public function __construct($host{
  7.           parent::__construct()// This call is important!
  8.           $this->handle fsockopen($host80);
  9.           stream_set_blocking($this->handle0);
  10.           fputs($this->handle"GET / HTTP/1.0\r\nConnection: close\r\n\r\n");
  11.       }
  12.  
  13.       public function step({
  14.           $this->response .= fread($this->handle1024);
  15.       }
  16.  
  17.       public function isFinished({
  18.           return feof($this->handle);
  19.       }
  20.  
  21.       public function getResponse({
  22.           return $this->response;
  23.       }
  24.  
  25.   }
  26.  
  27.   $thread1 new NetworkThread('www.nongnu.org/ubook/');
  28.   $thread2 new NetworkThread('ubook.asta-bielefeld.de');
  29.  
  30.   Thread::joinAll();
  31.  
  32.   // The responses begin with 'HTTP'.
  33.   echo substr($thread1->getResponse()04)/// HTTP
  34.   echo substr($thread2->getResponse()04)/// HTTP

Located in /Thread.php (line 77)


	
			
Method Summary
static void joinAll ()
Thread __construct ()
void isFinished ()
void join ()
void step ()
Methods
static method joinAll (line 86)

Runs all threads until they are finished.

While execution new threads can be created and will be executed, too.

  • access: public
static void joinAll ()
Constructor __construct (line 109)

Creates a new thread.

Important: Your subclass has to call parent::__construct().

  • access: protected
Thread __construct ()
isFinished (line 131)

Returns True, if the execution is finished.

  • abstract:
  • access: public
void isFinished ()
join (line 117)

Blocks until this thread is finished. Other threads are executed, too.

But it is unknown, if they are finished or not.

  • access: public
void join ()
step (line 126)

Makes only one step in your computation.

  • abstract:
  • access: public
void step ()

Documentation generated on Sat, 08 Oct 2011 17:00:53 +0200 by phpDocumentor 1.4.3