0

I want to use some threads to do my computations and my web service work faster. don't go deep on calculations, it works correctly. However, my problem is without using this thread it was taking something around 55 sec although now it's something around 55 sec again.

So I'm wondering am I using it in a right way? Is this correct form of using pthreads?

My php version : 5.5.12

class workerThread extends Thread {
    public function __construct($eng,$year,$capacity){
        $this->eng = $eng;
        $this->year = $year;
        $this->capacity = $capacity;
    }

    public function run(){

        $counter=0;
        $paye1=0;
        $paye2=0;
        $paye3=0;

        foreach($this->eng as $engs){
        $data=filter_engs($engs['id'],$this->year,$this->capacity);

        if(isset($data['state'])){






            if($engs['checkLevel']=='one'){
                $paye1++;
                $list[$counter]['avgCap']='2304';
                $list[$counter]['avg']=ceil(($data['cheked']/2304)*100);
                $list[$counter]['fullavg']=ceil(($data['cheked']/$data['allWork'])*100);
            }else if($engs['checkLevel']=='two'){
                $paye2++;
                $list[$counter]['avgCap']='1728';
                $list[$counter]['avg']=ceil(($data['cheked']/1728)*100);
                $list[$counter]['fullavg']=ceil(($data['cheked']/$data['allWork'])*100);
            }else if($engs['checkLevel']=='three'){
                $paye3++;
                $list[$counter]['avgCap']='1152';
                $list[$counter]['avg']=ceil(($data['cheked']/1152)*100);
                $list[$counter]['fullavg']=ceil(($data['cheked']/$data['allWork'])*100);
            }
            $list[$counter]['allWork']=$data['allWork'];
            $list[$counter]['wCap']=$data['wCap'];
            $list[$counter]['cheked']=$data['cheked'];
            $list[$counter]['design']=$data['wCap']-$data['cheked'];
            $list[$counter]['countCheked']=$data['countCheked'];
            $list[$counter]+=$engs;


            $counter++;


        }
    }
        return $list;
    }
}

And this is how I use it:

 $worker=new workerThread($eng,$year,$cap);
 $list=$worker->run();
 $worker->kill();

 //and doing some stuff with $list array

1 Answers1

0

You should not call run() directly as it will run the code in the main thread.

You should call start() which will create a new thread instead, and this new thread will execute run().

You can use join() later to wait for the threads to finish their work.

Additionally, run() shouldn't return anything. You should use shared resources and synchronization to make the finished state available to the main thread.


Example:

<?php

class WorkerThread extends \Thread
{
    private $eng;
    private $year;
    private $capacity;
    private $list = array();

    public function __construct($eng, $year, $capacity)
    {
        $this->eng = $eng;
        $this->year = $year;
        $this->capacity = $capacity;
    }

    /**
     * Replace with your code
     */
    public function run()
    {
        sleep(2); // thread running this will sleep for 2 seconds
        $this->list = array('chocolate', 'banana', 'pistachio');
    }

    public function getList()
    {
        return $this->list;
    }
}

$worker = new WorkerThread("Lorem", "Ipsum", "FooBar");
var_dump($worker->getList()); // should be empty
$worker->start();

// ... do some other stuff ...

$worker->join(); // main thread waits for WorkerThread to complete
var_dump($worker->getList()); // should have "chocolate", "banana", "pistachio"
Technomad
  • 317
  • 5
  • 13
  • Thank you so much. Can you give me a usefull sample? how can i return my result array to first thread again ... ? – J4F Codder Dec 22 '15 at 11:33
  • Glad it helped. I found http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications with an answer with a real world example. Does it help? – Technomad Dec 22 '15 at 11:36
  • emmmm, no! not exactly... but i appreciate that, thank you... i'll stick to your answer about run (),join() and other stuff untill it work. – J4F Codder Dec 22 '15 at 11:57
  • Ah I see your edit now. If you only have a thread you could just initialize the array in the main thread and pass it by reference to the new thread, and have no problems on integrity as long as you don't touch the shared array in the main thread while the new thread is executing. I found this more elaborate example for an environment where multiple threads share a resource http://stackoverflow.com/questions/32341744/php-pthreads-shared-objects . I'll try to add an example for your context later. – Technomad Dec 22 '15 at 12:06
  • Thank you so much i appreciate your help – J4F Codder Dec 22 '15 at 12:09
  • Thinking about your specific case, you could even create a getter in your worker for the said array and call it in the main thread **after** the worker has finished. You just need to have the array initialized with the worker and not the run method. – Technomad Dec 22 '15 at 12:36
  • well i have to mention this is the first time i'm trying to use threads , so i don't know how... just a simple sample could help me a lot to realized how these working... although i'm still searching and reading but a simple sample of getter in a worker would help cause i'm not even sure that are you talking about worker class as extention or some how i could define a worker in a extended thread – J4F Codder Dec 22 '15 at 12:56
  • I added an example you should be able to transpose to context. – Technomad Dec 22 '15 at 17:01