Reputation: 409
Please assist if possible. I intended to pull an X amount of rows from a DB, separating them into arrays of 20 arrays and pass them to a thread for processing simultaneously.
To ensure the process was working simultaneously i created a quick thread that echo's out the thread number then counts to 20. I expected to see results like "1 at 1" then "2 at 1". Instead i see the first thread counts to 20 before the second thread begins to execute. ie "1 at 1" ... "1 at 20" then only "2 at 1".
<?php
class helloworld extends Thread {
public function __construct($arg){
$this->arg = $arg;
}
public function run(){
if($this->arg){
for ($i=1;$i<=20;$i++){
echo $this->arg ." AT ";
echo $i." ";
sleep(1);
}
}
}
}
?>
Then to call it i use
for ($i=1;$i<=$num_threads;$i++){
$thread[$i] = new helloworld($i);
if($thread[$i]->start())
$thread[$i]->join();
}
Is what i am seeing correct? Or am i doing something stupid here?
Thanks
Rob
Upvotes: 2
Views: 1907
Reputation: 12932
The pthread's join() function waits for the thread specified to terminate. If that thread has already terminated, then pthread's join() function returns immediately. The thread specified must be joinable.
So, you are waiting for each started thread to terminate before moving on in your loop.
Upvotes: 3