0

I want to run PHP Thread class code on Windows. I tried some manual installation guides like e.g. this one but they didn't work. PHP cannot load the pthreads DLL even though the file exists at this location:

PHP Warning:  PHP Startup: Unable to load dynamic library 'php_pthreads.dll' (tried: C:\xampp\php\ext\php_pthreads.dll (The specified module could not be found), C:\xampp\php\ext\php_php_pthreads.dll.dll (The specified module could not be found)) in Unknown on line 0

I downloaded php_pthreads-3.1.6-7.0-ts-vc14-x64.zip. My PHP version is: PHP 8.1.6 (cli) (built: May 11 2022 08:55:59) (ZTS Visual C++ 2019 x64). Generally I'm still not entirely sure which pthread version to select since there are many choices here. Are any of them even going to work since they seem a bit outdated? My phpinfo says:

Can I maybe just use Windows threads since they are already available on my system?

After the pthreads installation, when I run my Thread class example 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();
?>

I still get the warning above and the following error: PHP Fatal error: Uncaught Error: Class "Thread" not found

What am I doing wrong? Is there maybe a way to do it with less "manual" effort in e.g. 1 composer command or an easier way to get threading working?

BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • 1
    Not sure what your use case is and if it will suffice, but have you looked at the [parallel](https://www.php.net/manual/en/book.parallel.php) functionality? I see you tried using phtreads, but it is deprecated in favor of parralel, see the [docs](https://www.php.net/manual/en/intro.pthreads.php#:~:text=pthreads%20is%20an%20object%2Dorientated,Threads%2C%20Workers%20and%20Threaded%20objects.&text=This%20extension%20is%20considered%20unmaintained%20and%20dead.&text=Consider%20using%20parallel%20instead.). – Michel Rummens Jul 14 '22 at 10:13
  • Maybe this can help? https://www.php.net/manual/en/pthreads.installation.php#115004 – aramcpp Jul 14 '22 at 10:21

0 Answers0