Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слева Предыдущая версия
Следующая версия
Предыдущая версия
php:pthreads [2017/01/24 17:21] – [Примеры] mirocowphp:pthreads [2017/01/25 00:46] (текущий) – [Статьи] mirocow
Строка 1: Строка 1:
 {{tag>php languages extension}} {{tag>php languages extension}}
  
-====== Threading for PHP - Share Nothing ======+====== Pthread (Threading for PHP - Share Nothing======
  
   * https://github.com/krakjoe/pthreads ([[https://github.com/krakjoe/pthreads/tree/PHP5|5.x]] - [[https://github.com/krakjoe/pthreads|7.x]])   * https://github.com/krakjoe/pthreads ([[https://github.com/krakjoe/pthreads/tree/PHP5|5.x]] - [[https://github.com/krakjoe/pthreads|7.x]])
Строка 7: Строка 7:
   * https://github.com/krakjoe/pthreads-polyfill   * https://github.com/krakjoe/pthreads-polyfill
  
 +===== Установка =====
 +
 +==== Brew (MacOS) ====
 +
 +<code bash>
 +$ brew install php70-pthreads
 +</code>
 +
 +==== pecl ====
 +
 +<code bash>
 +$ pecl install pthread
 +</code>
 +
 +==== Docker ====
 +
 +<code bash>
 +$ docker pull jdecool/php-pthreads
 +</code>
 ===== Примеры ===== ===== Примеры =====
  
Строка 233: Строка 252:
 ?> ?>
 </code> </code>
 +
 +<code php>
 +class Wallet{
 +    public $balance;
 +    public function __construct($money){
 +        $this->balance = $money;
 +    }
 +    public function getBalance(){
 +        return $this->balance;
 +    }
 +    public function setBalance($value){
 +        $this->balance = $value;
 +    }
 +}
 +class MyThread extends Thread{
 +    private $wallet;
 +    private $std;
 +    public function __construct($wallet,$std){
 +        $this->wallet = $wallet;
 +        $this->std = $std;
 +    }
 +    public function run(){
 +        $this->synchronized(function($thread){
 +                $hack = $this->wallet;
 +                if($hack->getBalance() - 80 >0){
 +                    sleep(1);
 +                    $hack->setBalance($hack->getBalance() - 80);
 +                    echo $this->getThreadId() . "reduce 80 successful<br/>Current num is:" . $hack->getBalance() . "<Br/>";
 +                    //Here is Wrong!  The result is bool(false)????!!!!
 +                    var_dump($hack == $this->wallet);
 +                }
 +                 else
 +                     echo $this->getThreadId() . "reduce fail<br/>Current num is:" . $hack->getBalance() . "<br/>";
 +            
 +        },$this->std);
 +    }
 +}
 +$wallet = new Wallet(200);
 +$std = new stdClass();
 +for($x=0;$x<3;$x++){
 +    $pool[] = new MyThread($wallet,$std);
 +    $pool[$x]->start();
 +}
 +</code>
 +
 +===== Примеры использования / Помошники (IDE Helpers) =====
 +
 +  * https://github.com/zerustech/pthreads-tutorial
 +  * https://github.com/unusorin/pthreads (IDE support for pthreads)
 +  * https://github.com/krakjoe/promises
 ===== Документация ===== ===== Документация =====
  
Строка 241: Строка 310:
   * https://habrahabr.ru/post/300952/   * https://habrahabr.ru/post/300952/
   * [[https://habrahabr.ru/post/193270/|PHP IPC — Межпроцессное взаимодействие в PHP]]   * [[https://habrahabr.ru/post/193270/|PHP IPC — Межпроцессное взаимодействие в PHP]]
 +  * [[https://habrahabr.ru/post/75454/|Почти настоящая многопоточность средствами php 5]]
 +  * http://www.smddzcy.com/2016/01/tutorial-multi-threading-in-php7-pthreads/
 +  * https://blog.madewithlove.be/post/thread-carefully/