Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP pthreads: Fatal error: Class 'Thread' not found

I use php5.5 on my webserver. Now I want to use pthreads. Here's my php config: http://dd19010.kasserver.com/infophp.php55

After implementing this code.....

 <?php

class AsyncOperation extends Thread
{
    public function __construct($threadId)
    {
        $this->threadId = $threadId;
    }

    public function run()
    {
        printf("T %s: Sleeping 3sec\n", $this->threadId);
        sleep(3);
        printf("T %s: Hello World\n", $this->threadId);
    }
}

$start = microtime(true);
for ($i = 1; $i <= 5; $i++) {
    $t[$i] = new AsyncOperation($i);
    $t[$i]->start();
}
echo microtime(true) - $start . "\n";
echo "end\n";

?>

... the problem is this very error: Fatal error: Class 'Thread' not found in. Do I have to include some include_once or something similar to make it work? What do I have to do??

like image 212
ItsMeDom Avatar asked Apr 07 '14 00:04

ItsMeDom


3 Answers

Hi I encountered this problem and managed to solve it.

First, consider the VC version of your PHP and the VC version of extension. In mine I attached the extension pthreads.dll with version VC14 from http://windows.php.net/downloads/pecl/releases/pthreads/ but my PHP VC version is VC11. Look for the lower version to match with the VC version of your PHP.

Second, maybe you missed the step #3 at PHP page. It states that you need to copy the pthreadVC2.dll to different folder. Here's the full instruction.

  1. Find out what is your 'PHP Extension Build' version by using phpinfo(). You can use this - http://localhost/?phpinfo=1

  2. Download the pthreads that matches your php version (32 bit or 64 bit) and php extension build (currently used VC11). Use this link for download - http://windows.php.net/downloads/pecl/releases/pthreads/

  3. Extract the zip - Move php_pthreads.dll to the 'bin\php\ext\' directory. Move pthreadVC2.dll to the 'bin\php\' directory. Move pthreadVC2.dll to the 'bin\apache\bin' directory. Move pthreadVC2.dll to the 'C:\windows\system32' directory.

  4. Open php\php.ini and add extension=php_pthreads.dll

Reference: https://secure.php.net/manual/en/pthreads.installation.php

like image 198
Player1 Avatar answered Nov 08 '22 05:11

Player1


1) Create one php file
phpinfo(); --> Run
Example: Info
PHP Version: 5.6.31
Compiler: MSVC11 (Visual C++ 2012)
Architecture: x64

2)Go to website:
http://windows.php.net/downloads/pecl/releases/pthreads/
Example 2.0.9 file
Compiler:VC11
Architecture:x64
php_pthreads-2.0.9-5.6-ts-vc11-x64.zip download.

3)Extract php_pthreads.dll and pthreadVC2.dll.
wamp\bin\php\php5.6.31\ext\ --> copy php_pthreads.dll
wamp\bin\php\php5.6.31\ --> copy pthreadVC2.dll
wamp\bin\apache\apache2.4.27\bin --> copy pthreadVC2.dll

4)Now edit php.ini
wamp\bin\apache\apache2.4.27\bin\php.ini\ --> Add extension=php_pthreads.dll
wamp\bin\php\php5.6.31\php.ini\ --> Add extension=php_pthreads.dll

5)Now Restart Wamp

like image 6
Murat Çakmak Avatar answered Nov 08 '22 06:11

Murat Çakmak


Your phpinfo shows that you have php with thread safety disabled. You need to install a version of php that is thread safe to use pthreads. This may or may not fix your current issue though.

You may need to copy the pthreadsVC2.dll into the bin directory of your web service as well.

/etc/php55/fpm/

You're looking for the folder with php.ini in it.

Make sure the php.ini file has the line added:

extension=php_pthreads.dll
like image 5
Kyohei Kaneko Avatar answered Nov 08 '22 06:11

Kyohei Kaneko