Skip to content

Instantly share code, notes, and snippets.

@alkavan
Last active July 29, 2017 07:01
Show Gist options
  • Save alkavan/7485335 to your computer and use it in GitHub Desktop.
Save alkavan/7485335 to your computer and use it in GitHub Desktop.
How to install the PHP pthreads extension on Arch Linux x86_64

I wanted to test the new stable pthreads extension for PHP on my Arch Linux 64-bit. to install the pthreads extension using pecl you are required to use a compiled version of PHP with the the thread safety support flag --enable-maintainer-zts. the most clean way on Arch would be then rebuild the original package with the flag. this was very important to me to keep my PHP as stable as possible. so here's what you should do ...

First checked what packages dependent on the 'php' package:

pacman -Qii php

Got the following outout:
:: php-apache: requires php
:: php-apcu: requires php
:: php-mcrypt: requires php
:: php-pear: requires php
:: php-pgsql: requires php
:: xdebug: requires php

Remove all of them including 'php', so:

pacman -R php php-apache php-apcu php-mcrypt php-pear php-pgsql xdebug

Clone Archlinux's official package repository

cd /opt/
git clone git://projects.archlinux.org/svntogit/packages.git
cd packages/php/trunk

Make changes to add threads support

vim PKGBUILD

should look something like

...
--with-xsl=shared \
--with-zlib \
--enable-maintainer-zts
...

Make the new packages

makepkg -s

Install the packages you removed

pacman -U \
php-5.5.6-1-x86_64.pkg.tar.xz \
php-apache-5.5.6-1-x86_64.pkg.tar.xz \
php-mcrypt-5.5.6-1-x86_64.pkg.tar.xz \
php-pear-5.5.6-1-x86_64.pkg.tar.xz \
php-pgsql-5.5.6-1-x86_64.pkg.tar.xz \
php-tidy-5.5.6-1-x86_64.pkg.tar.xz 

Install pthreads

pecl install pthreads

Install the 'apcu' package for APC support

cd ../../php-apcu/trunk
makepkg -si

Test pthreads using this code, should get you started:

<?php
class Data extends Stackable
{
    //private $name;
	public function __construct($_name) {
		// if you set any variable, workers will get the variable, so do not set any variable
//		$this->name = $_name;
		echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);
	}
	public function run(){
		echo __FILE__.'-'.__LINE__.'<br/>'.chr(10);
	}
}

class ReadWorker extends Worker
{
	public function __construct(&$_data) {
		$this->data = $_data;
	}
	public function run() {
		while(TRUE)
		{
			if ($arr = $this->data->shift()) // receiving datas
			{
				echo 'Received data:'.print_r($arr,1).chr(10);
			} else {
				usleep(50000);
			} 
		}
	}
}

class WriteWorker extends Worker
{
	public function __construct(&$_data) {
		$this->data = $_data;
	}
	
	public function run() {
		while(1)
		{
			$this->data[] = array(time(),rand()); // write data
			usleep(rand(50000, 1000000));
		}
	}
}

$data   = new Data('');
$reader = new ReadWorker($data);
$writer = new WriteWorker($data);

$reader->start();
$writer->start();

THE END!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment