16

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??

ItsMeDom
  • 540
  • 3
  • 5
  • 18
  • 2
    Are you very sure the module is loaded into PHP? According to the phpinfo nothing related to threads can be found. – Paul Apr 24 '14 at 12:57

6 Answers6

8

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

Player1
  • 2,878
  • 2
  • 26
  • 38
7

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

Murat Çakmak
  • 151
  • 2
  • 7
5

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
Kyohei Kaneko
  • 932
  • 2
  • 8
  • 25
  • This is correct, the first problem is you're running nts PHP ... sort that first ... – Joe Watkins May 07 '14 at 04:12
  • I contacted my webhost provider and they're saying they cannot offer php with thread safety turned on... But I need threading so badly... Is there any other way? – ItsMeDom May 07 '14 at 05:52
  • The only way would be to switch Web hosts then. So you need to weigh up whether multi threading is so vital to you're project that it's worth the upheaval of the Web host switch or whether there's another implementation you can use – Kyohei Kaneko May 07 '14 at 08:31
  • Well @KyoheiKaneko, do you know any other threading options in php? – ItsMeDom May 09 '14 at 09:17
  • I don't believe there are any other multi threading options available for php afaik. And if there was it is extremely likely that they would also require thread safety. If you need multi threading you need thread safe php. – Kyohei Kaneko May 09 '14 at 17:38
2

I am using WAMP and found that the pthreadVC2.dll should go to the Apache folder instead:

C:\wamp\bin\apache\apache2.4.9\bin

Unlike what is written in README.md, you don't need to have it in the PHP folder, but the php_pthreads.dll should still go to:

C:\wamp\bin\php\php5.5.12\ext

After this, search in this file:

C:\wamp\bin\apache\apache2.4.9\bin\php.ini

For ;extension=php_pgsql.dll and add extension=php_pthreads.dll in a new line after it (yes, it's the bin\php.ini in the Apache folder, not the one in the PHP folder).

Exit WAMP and start it again. You should now see in WAMP menu under PHP > PHP extensions, the new php_pthreads extension.

Armfoot
  • 4,663
  • 5
  • 45
  • 60
  • Thank you! Now i can load the pthreads extension on my Windows environment. Sincerly I don't understand why the dll should be into the apache folder and not into the php folder, but it works :-) – Stefano P. Jun 29 '16 at 12:05
0

I think you need to include the extensions int he php.ini file, because I can't see it in the config. You can see that each library has its own section like MySQL, but there isn't such for the threads. I haven't used threads ever but that should be a good place to start from.

Hristo Petev
  • 309
  • 3
  • 13
0

By default Threads are not implemented in PHP, and according to your phpinfo it does not seem to be loaded. Check out the PHP manual on how to set-up/configure the module.

Paul
  • 471
  • 4
  • 10