PHP code example of devmastersbv / pthreads

1. Go to this page and download the library: Download devmastersbv/pthreads library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devmastersbv / pthreads example snippets


class Task extends \devmastersbv\pthreads\Task {
    private $logger;

    public function run(){
        $this->logger = $this->worker->getLogger();
        $this->logger->log("Message",["This will be var_dumped"]);
        $this->logger->log(function($var){
            //do whatever you like with var, synchronized.
        },$var);

        //Increment data by 1
        $this->data->storeCounter("total",1);
        $this->setGarbage();
    }
}

$logger = new \devmastersbv\pthreads\SafeLog;
$pool = new \Pool(4, "devmastersbv\\pthreads\\Worker", [$logger, PTHREADS_INHERIT_NONE, "vendor/autoload.php"]);
$data = new \devmastersbv\pthreads\Data;
$pool->submit(new Task($data));
while($pool->collect(function(\Collectable $task){
    return $task->isGarbage();
})){
    continue;
}
$pool->shutdown();
var_dump($data->total);
//Will display int(4)