POSIX Threads, better known as pthreads, is a library that allows a computer program to execute multiple processes or threads concurrently by forking child processes from its parent process. The pthreads library can be used in PHP, making it therefore possible to fork processes in the background while executing something else simultaneously. Thus, multithreading is another way to cope with latency in I/O calls. In order to accomplish this, we will need a thread-safe version of PHP with the pthreads extension enabled. In our case, we will use a Linux for PHP container that is running a Zend thread-safe (ZTS) version of PHP 7.0.29. Open a new Terminal window, cd into the project's directory and enter the following command:
# docker run -it --rm \ > -p 8282:80 \
> -v ${PWD}/:/srv/fasterweb \ > asclinux/linuxforphp-8.1:7.0.29-zts \ > /bin/bash
Once you are done entering this command, you should see the following information if you enter the php -v command in the CLI:

This message confirms we are using a thread-safe (ZTS) version of PHP. Then, on the container's CLI, enter these commands:
# mv /srv/www /srv/www.OLD
# ln -s /srv/fasterweb/chapter_4 /srv/www
# cd /srv/www
# pecl install pthreads
# echo "extension=pthreads.so" >> /etc/php.ini
You can now check that the pthreads extension is properly installed by entering the command php -i. The last command should allow you to see the extension's version number. If this is the case, then the extension was installed correctly:

Now that the pthreads library is installed and enabled, let's proceed to use it by trying to create multiple threads that will truly execute simultaneously on the computer's CPUs. To do so, we will use the following source code:
// chap4_pthreads.php $start = microtime(true); class TestThreads extends Thread { protected $arg; public function __construct($arg) { $this->arg = $arg; } public function run() { if ($this->arg) { $sleep = mt_rand(1, 10); printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg,
$sleep); sleep($sleep); printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg); } } } $stack = array(); // Create Multiple Thread foreach ( range('1', '9') as $id ) { $stack[] = new TestThreads($id); } // Execute threads foreach ( $stack as $thread ) { $thread->start(); } sleep(1); $time = microtime(true) - $start; echo 'Time elapsed: ' . $time . PHP_EOL; echo memory_get_usage() . ' bytes' . PHP_EOL;
Once executed, we obtain the following output:

The results clearly show that the threads were executed simultaneously as the total elapsed time for the script was 10 seconds even though each thread slept for at least a few seconds. If this synchronous blocking code was executed without multithreading, it would have taken approximately 40 seconds in all to complete execution. Multitasking would not have been an appropriate solution in this case, as the blocking calls to the sleep() function would have prevented each generator from yielding control to the main loop.
Now that we have seen both multitasking via asynchronous programming and multithreading via the POSIX Threads library, we will turn our attention to a PHP library that can be very useful when it comes to programming asynchronously, namely the ReactPHP library.