moinul15
moinul15

Reputation: 41

How to use Thread in PHP on Windows

I want to use thread in PHP . I am using windows . What needs to be done to do this . Here is the code i am running .

<?php
class AsyncOperation extends Thread {
  public function __construct($arg){
    $this->arg = $arg;
  }

  public function run(){
    if($this->arg){
      printf("Hello %s\n", $this->arg);
    }
  }
}
$thread = new AsyncOperation("World");
if($thread->start())
  $thread->join();
?>

When i run the code it shows

Fatal error: Class 'Thread' not found in D:\xampp\htdocs\my.php on line 2

Thanks in advance

Upvotes: 3

Views: 8338

Answers (1)

Jens A. Koch
Jens A. Koch

Reputation: 41737

It seems like the pthreads extension is not installed on your system. It's a custom PHP extension not by default installed with XAMPP. Go fetch it.

You find pthread releases for Windows over at http://windows.php.net/downloads/pecl/releases/pthreads/

Add pthreadVC2.dll to the same directory as php.exe, e.g. C:\xampp\php Add php_pthreads.dll to PHP extention folder, eg. C:\xampp\php\ext

Then modify php.ini by adding extension=php_pthreads.dll in the extensions section.

The code you posted is a basic example, which should work right out of the box, when the extension is installed.


And a goodie on top: Video by Joe Watkins explaining "Parallel PHP"

Upvotes: 5

Related Questions