3

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

moinul15
  • 41
  • 1
  • 4

1 Answers1

5

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"

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • 1
    I just did what you told me to do . but still shows same error . Is there any other thing i need to install ? I am using XAMPP 5.6.3 and dowloaded php_pthreads-2.0.9-5.6-ts-vc11-x86.zip and pasted two specified files in the specific folder . – moinul15 Jan 07 '15 at 18:02
  • 1
    Yes. You need to modify php.ini - the extensions section and include php_ptreads.dll there. Just search for ".dll" in php.ini. and add it at the bottom. – Jens A. Koch Jan 07 '15 at 18:19
  • 1
    thank you . solved the problem . Had to add "extension=php_pthreads.dll" to php.ini – moinul15 Jan 07 '15 at 18:23
  • I have the same problem but adding extension=php_pthreads.dll didn´t solve it – UrielUVD Jul 07 '16 at 21:04