Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pthread not working in php

I have downloaded the PHP Pthreads dll file from http://windows.php.net/downloads/pecl/releases/pthreads/ and enabled it in php.ini as below:

extension=pthreadVC2.dll
extension=php_pthreads.dll

I have used below sample code:

<?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 executed the code i get the following error:

Fatal error: Class 'Thread' not found in C:\htdocs\threads\AsyncOperation.php on line 2 Call Stack: 0.0008 333464 1. {main}() C:\htdocs\threads\AsyncOperation.php:0

like image 305
Cindrella Avatar asked Dec 07 '22 08:12

Cindrella


2 Answers

There are two issues here:

1) First have to look for dll files location correctly. dll files should be placed as below:

C:\PHP5\pthreadVC2.dll
C:\PHP5\ext\php_pthreads.dll

and in php.ini file only php_pthreads.dll should be enabled as

extension=php_pthreads.dll

2) Have to look for Versions of PHP and dll file.

My PHP is VC6 build and dll file used is VC9. Thats why module didn't get installed. I came to know this difference by using "php -m".

Since there is no VC6 build of dll file, I have used VC9 build of PHP and used pthreads and the program is working perfectly.

Note:The above two solutions solved my problems. But if you are still getting errors check if you have debuggers enabled xdebug or zend. Disable them and try again.

like image 192
Cindrella Avatar answered Dec 16 '22 08:12

Cindrella


If you have installed PHP on a different folder rather than C:/PHP5, it is good to add pthreadVC2.dll to httpd.conf. Otherwise, pthreads extension module can not find it.

LoadFile "c:/not_default_php5/pthreadVC2.dll"

NOTE: If after adding LoadFile, still Apache can not find DLL, just remove the Loadfile line and copy the DLL to Apache bin folder.

c:/apache_home/bin/pthreadVC2.dll
like image 22
user2292916 Avatar answered Dec 16 '22 08:12

user2292916