标签归档:pthreads

PHP7.3 zts(线程安全)编译以及多线程(pthreads)使用

下载PHP

下载地址:https://www.php.net/downloads.php,开始下载:

su root
mkdir tmp
cd tmp
wget https://www.php.net/distributions/php-7.3.7.tar.bz2

编译安装PHP开启线程安全

tar -jxvf php-7.3.7.tar.bz2
cd php-7.3.7
yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libicu-devel

编译

./configure \
--prefix=/usr/local/php-7.3.7-ts \
--enable-maintainer-zts \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-bcmath \
--enable-exif \
--enable-intl \
--enable-mbstring \
--enable-pcntl \
--with-pdo-mysql \
--enable-sockets \
--enable-mysqlnd \
--with-gd

然后:

make && make install

更新autoconf

Autoconf version 2.68 or higher is required错误是才需要此步骤

wget http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
tar -zxvf autoconf-latest.tar.gz 
cd autoconf-2.69/
.configure
make
make install
autoconf --version 

安装pthreads

git clone https://github.com/krakjoe/pthreads.git
cd pthreads
/usr/local/php-7.3.7-ts/bin/phpize
./configure --with-php-config=/usr/local/php-7.3.7-ts/bin/php-config
make
make install

修改 /usr/local/php-7.3.7-ts/lib/php.ini,增加 extension=pthreads

测试pthreads

/usr/local/php-7.3.7-ts/bin/php worker.php

<?php

class MyWorker extends Worker
{
    public $id;
    public $time;

    public function __construct($id, $time)
    {
        $this->id = $id;
        $this->time = $time;
    }

    public function run()
    {
        while (true) {
            echo sprintf(worker %s is running, %s\n, $this->id, date('Y-m-d H:i:s'));
            sleep($this->time);
        }
    }
}

class MyManager
{
    /**
     * @var MyWorker[]
     */
    protected $workers;

    public function add(MyWorker $work)
    {
        $this->workers[$work->id] = $work;
        echo 'add worker' . $work->id . \n;
    }

    public function run()
    {
        foreach ($this->workers as $id => $work) {
            echo 'run worker' . $work->id . \n;
            $work->start();
        }
    }
}

$manager = new MyManager();
$manager->add(new MyWorker('1', 1));
$manager->add(new MyWorker('2', 3));
$manager->run();