After Long Time I am back. Today I am going to write about how we can code multi Thread application using PHP(Window Environment). As we all know php current version 5.6 does not has multithread. Hmm so, how we can do it :-(. Don't worry I have a solution after googling a lot. Without wasting of time let me start.
I am assuming that you have xamp install in your system. I have XAMPP installed and PHP version is PHP VC11 x86 Thread Safe: 5.6.8.
Now you need to download PThreads from link https://www.sourceware.org/pthreads-win32/ (Win32) or http://windows.php.net/downloads/pecl/releases/pthreads/ (Win64)
After Download zip, extract it. Here you need two dll.
1. pthreadVC2.dll
2. php_pthreads.dll
Copy paste the file 'pthreadVC2.dll' to php extension directory. My is 'C:\xampp\php\ext'. After that copy paste 'php_pthreads.dll' to php directory. My is 'C:\xampp\php' but sometime you may need to paste it to Windows folder Window folder, My is 'C:\Windows'. Now Restart the Apache.
Write the code to test our multithread installation.
<?php
class AsyncOperation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$sleep = mt_rand(1, 10);
printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
echo '<br/>';
sleep($sleep);
printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg);
echo '<br/>';
}
}
}
// Create a array
$stack = array();
//Iniciate Miltiple Thread
foreach ( range("A", "D") as $i ) {
$stack[] = new AsyncOperation($i);
}
// Start The Threads
foreach ( $stack as $t ) {
$t->start();
}
?>
OUTPUT :
1:49:56pm: C -start -sleeps 1
1:49:57pm: C -finish
1:49:56pm: A -start -sleeps 4
1:50:00pm: A -finish
1:49:56pm: D -start -sleeps 6
1:50:02pm: D -finish
1:49:56pm: B -start -sleeps 8
1:50:04pm: B -finish
I am assuming that you have xamp install in your system. I have XAMPP installed and PHP version is PHP VC11 x86 Thread Safe: 5.6.8.
Now you need to download PThreads from link https://www.sourceware.org/pthreads-win32/ (Win32) or http://windows.php.net/downloads/pecl/releases/pthreads/ (Win64)
After Download zip, extract it. Here you need two dll.
1. pthreadVC2.dll
2. php_pthreads.dll
Copy paste the file 'pthreadVC2.dll' to php extension directory. My is 'C:\xampp\php\ext'. After that copy paste 'php_pthreads.dll' to php directory. My is 'C:\xampp\php' but sometime you may need to paste it to Windows folder Window folder, My is 'C:\Windows'. Now Restart the Apache.
Write the code to test our multithread installation.
<?php
class AsyncOperation extends Thread {
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$sleep = mt_rand(1, 10);
printf('%s: %s -start -sleeps %d' . "\n", date("g:i:sa"), $this->arg, $sleep);
echo '<br/>';
sleep($sleep);
printf('%s: %s -finish' . "\n", date("g:i:sa"), $this->arg);
echo '<br/>';
}
}
}
// Create a array
$stack = array();
//Iniciate Miltiple Thread
foreach ( range("A", "D") as $i ) {
$stack[] = new AsyncOperation($i);
}
// Start The Threads
foreach ( $stack as $t ) {
$t->start();
}
?>
OUTPUT :
1:49:56pm: C -start -sleeps 1
1:49:57pm: C -finish
1:49:56pm: A -start -sleeps 4
1:50:00pm: A -finish
1:49:56pm: D -start -sleeps 6
1:50:02pm: D -finish
1:49:56pm: B -start -sleeps 8
1:50:04pm: B -finish