Getting Started

First thing is first, here's a mantra, repeat it:

Efficient Multi-Threading is about using minimum threads to do maximum work.

When setting out in the world of multi-threading, one might be tempted to just throw threads at something and expect that it will perform better. This is rarely the case in the real world. Efficient use of Multi-Threading relies on your ability to minimize on resources in order to maximize on your hardware. In this tutorial, I take a mundane task - fetching an external web page - and tutor on the best ways to multi-thread that task in the real world.

The Simplest of All Worlds

In the simplest of all worlds, we create a Thread in order to fetch content asynchronously.

                class WebRequest extends Thread {
                    public $url;
                    public $response;
                    
                    public function __construct($url){
                        $this->url = $url;
                    }
                    
                    public function run() {
                        $this->response = file_get_contents($this->url);
                    }
                }
                
                $request = new WebRequest("http://pthreads.org");
                
                if ($request->start()) {
                    
                    /* do some work */
                    
                    /* ensure we have data */
                    $request->join();
                    
                    /* we can now manipulate the response */
                    var_dump($request->response);
                }
                

The problem with the above implementation is that the process is left to deal with the data, which is a big waste of resources. Can you see why ?

The Best of Both Worlds

A savvy designer will notice that the WebRequest Thread could and should perform manipulation of the data, such that the processing of the response can also be performed asynchronously, not just the download of data.

                class WebRequest extends Thread {
                    public $url;
                    public $data;
                    
                    public function __construct($url){
                        $this->url = $url;
                    }
                    
                    public function run() {
                        $response = file_get_contents($this->url);
                        if ($response) {
                            /* process response into useable data */
                            
                            $this->data = $response;
                        }
                    }
                }
                
                $request = new WebRequest("http://pthreads.org");
                
                if ($request->start()) {
                    
                    /* do some work */
                    
                    /* ensure we have data */
                    $request->join();
                    
                    /* we can now manipulate the response */
                    var_dump($request->data);
                }
                

The example above makes much more efficient use of Multi-Threading, omitted is the logic that takes care of parsing the data down into a useable (more importantly, shareable) form.