ScotlandPHP 2016

pthreads

add a note add a note

User Contributed Notes 3 notes

up
12
jasonrlester at yahoo dot com
2 years ago
Note that this extension *is* a high level implementation of POSIX threads, including on Windows (which is why pthreadsV*.dll is required)
up
4
admin at deosnet dot com
2 years ago
Hello,

WARNING : When using Stackable objects in callable functions by your Threads, you must be very careful if you use it as an array. Indeed, if you do not copy your Stackable "array" in a local variable, the execution time can drop drastically !

Also, if you want to modify an array() in a function, you will also store in a local variable in order that your array is in a thread-safe context.
up
0
r3x at engelhardt-stefan dot de
9 months ago
If you try to test threading, remember to let php think slow:

Skript: -- C:\Webserver\htdocs>php mttest.php

<?php
class My extends Thread{
    function
run(){
        for(
$i=1;$i<10;$i++){
            echo
Thread::getCurrentThreadId() .  "\n";
           
sleep(2);     // <------
       
}
    }
}

for(
$i=0;$i<2;$i++){
   
$pool[] = new My();
}

foreach(
$pool as $worker){
   
$worker->start();
}
foreach(
$pool as $worker){
   
$worker->join();
}
?>

Output: -- C:\Webserver\htdocs>php mttest.php
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816
6300
5816

If you leave sleep() out, the cpu-time for the threads is long enough to complete the script at once.
To Top