Pages

Saturday, March 16, 2013

PHP and multi-thread on Windows (pthreads configuration)

Note: Official PHPLobby page is open: http://phplobby.com/

Introduction

pthreads is an Object Orientated API that allows user-land multi-threading in PHP. It includes all the tools you need to create multi-threaded applications targeted at the Web or the Console. PHP applications can create, read, write, execute and synchronize with Threads, Workers and Stackables. Read More


Requirements
  1. PHP 5.3+ (5.4) x64 or x86 - Thread Safe (Releases)
  2. Apache httpd (Apache HTTP Server Project) (Releases) Optional
  3. pthread-w32 library (Releases)

How to configure from scratch step by step

In this tutorial i'm using:
    1. php-5.4.13-Win32-VC9-x86
    2. pthreads-0.42-5.4-ts-vc9-x86
1. Extract the content from archives and place in one folder: "C:\Users\Gizmo\Desktop\apachePhp\"
php pthreads directory
Root directory

2. Copy "pthreadVC2.dll" from "pthreads-0.42-5.4-ts-vc9-x86" Folder to "php-5.4.13-Win32-VC9-x86" Folder.
3. Copy "php_pthreads.dll" from "pthreads-0.42-5.4-ts-vc9-x86" Folder to "php-5.4.13-Win32-VC9-x86\ext" Folder
4. Find and rename "php.ini-development" file to "php.ini" in "php-5.4.13-Win32-VC9-x86" Folder
5. Open "php.ini" file with notepad or notepad++ and fine following text ";extension=php_shmop.dll" and add this configuration after it "extension=php_pthreads.dll" should look like:
...
;extension=php_pgsql.dll
;extension=php_pspell.dll
;extension=php_shmop.dll
extension=php_pthreads.dll
6. Find and remove the comment symbol ";" " ; extension_dir = "ext" " should look like:
; extension_dir = "ext" => extension_dir = "ext"
7. Save the "php.ini" file and close it.
8. Create "index.php" on your desktop and open with notepad or notepad++.
9. Place this code

<?php
class AsyncOperation extends Thread {
public function __construct($arg){
$this->arg = $arg;
}

public function run(){
if($this->arg){
for($i = 0; $i < 5; $i++) {
echo "-> " . $this->arg . "\n";
sleep(1);
}
}
}
}

flush();
$thread = new AsyncOperation("Thread 1");
$thread2 = new AsyncOperation("Thread 2");
$thread->start();
$thread2->start();
$thread->join();
$thread2->join();
?>

10. Save the file and close the editor (notepad / ++)
11. Open CMD (Command Prompt) and execute the following line:

  1. cd "C:\Users\ USER \Desktop\apachePhp\php-5.4.13-Win32-VC9-x86"
  2. php -q C:\Users\ USER \Desktop\index.php
12. Result should be like this:
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2
-> Thread 1
-> Thread 2



We are Done :) More info: github official website, Good Luck!

5 comments:

  1. Could you please provide an example on how to run a pthreads script from the browser, on windows?

    ReplyDelete
    Replies
    1. you should be able to run same script in browser over apache, it should work but i never tested it, i will try and will get back with more info for you.

      Delete
  2. Very Nice Tutorial Please if you can also provide an suggestions on how to run a pthreads script from the browser using wamp/xampp server localhost, on windows?

    ReplyDelete
    Replies
    1. I will make another post on the official web site (phplobby.com) about that. Thanks for suggestion and feedback.

      Delete
  3. Fatal error: Class 'Thread' not found in F:\xampp\htdocs\loc\test.php on line 2

    ReplyDelete