Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

When passing a thread object in worker to other methods, their first sibling in the stack is passed #603

Closed
zerustech opened this issue Jun 20, 2016 · 2 comments

Comments

@zerustech
Copy link

zerustech commented Jun 20, 2016

If you know how to write phpt tests, please open a PR with environment information, and your test, this will cause CI to test your issue before a human is able to look

Environment

  • PHP: 5.6.14
  • pthreads: 2.0.10
  • OS: Mac OS X 10.11.5

Summary

When passing a thread object in worker to other methods, their first sibling in the worker stack is passed.

Reproducing Code

<?php                                                                                                                    

class Inventory extends Threaded                                                                                         
{                                                                                                                        
    private $queue;                                                                                                      

    public function __construct(Threaded $queue)                                                                         
    {                                                                                                                    
        $this->queue = $queue;                                                                                           
    }                                                                                                                    

    public function put(Thread $producer, $product)                                                                      
    {                                                                                                                    
        $this->queue[] = $product;                                                                                       

        printf("[%s] instance [%s] in worker [%s] is producing now ...\n\n", get_class($producer), $producer->name, $producer->worker->name);
    }                                                                                                                    

}                                                                                                                        

class ProducerFoo extends Thread                                                                                         
{                                                                                                                        
    private $name;                                                                                                       

    public function __construct($name)                                                                                   
    {                                                                                                                    
        $this->name = $name;                                                                                             
    }                                                                                                                    

    public function run()                                                                                                
    {                                                                                                                    
        printf("[ProducerFoo] [%s] in worker [%s] is about to run ...\n", $this->name, $this->worker->name);             
        $this->worker->inventory->put($this, '*');                                                                       
    }                                                                                                                    
}                                                                                                                        

class ProducerBar extends Thread                                                                                         
{                                                                                                                        
    private $name;                                                                                                       

    public function __construct($name)                                                                                   
    {                                                                                                                    
        $this->name = $name;                                                                                             
    }                                                                                                                    

    public function run()                                                                                                
    {                                                                                                                    
        printf("[ProducerBar] [%s] in worker [%s] is about to run ...\n", $this->name, $this->worker->name);             
        $this->worker->inventory->put($this, '*');                                                                       
    }                                                                                                                    
}                                                                                                                                                                                                                                                                     
$queue = new Threaded();                                                                                                 
$inventory = new Inventory($queue);  

$p1 = new ProducerFoo('p1');                                                                                             
$p2 = new ProducerFoo('p2');                                                                                             
$p3 = new ProducerBar('p3');                                                                                             

$worker = new Worker();                                                                                                  
$worker->inventory = $inventory;                                                                                         
$worker->name = "w1";                                                                                                    

$worker->stack($p1);                                                                                                     
$worker->stack($p2);                                                                                                     
$worker->stack($p3);                                                                                                     

$worker->start();                                                                                                        
$worker->shutdown();  

Expected Output

[ProducerFoo] [p1] in worker [w1] is about to run ...
[ProducerFoo] instance [p1] in worker [w1] is producing now ...

[ProducerFoo] [p2] in worker [w1] is about to run ...
[ProducerFoo] instance [p1] in worker [w1] is producing now ...

[ProducerBar] [p3] in worker [w1] is about to run ...
[ProducerBar] instance [p3] in worker [w1] is producing now ...

Actual Output

[ProducerFoo] [p1] in worker [w1] is about to run ...
[ProducerFoo] instance [p1] in worker [w1] is producing now ...

[ProducerFoo] [p2] in worker [w1] is about to run ...
[ProducerFoo] instance [p1] in worker [w1] is producing now ...

[ProducerBar] [p3] in worker [w1] is about to run ...
[ProducerFoo] instance [p1] in worker [w1] is producing now ...
@zerustech
Copy link
Author

This works with PHP 7 and the V3 API

@zerustech zerustech changed the title Worker does not pass stacked objects with correct property values When passing a thread object in worker to other methods, their first sibling in the stack is passed Jun 20, 2016
@krakjoe
Copy link
Owner

krakjoe commented Jun 30, 2016

I'm no longer working on v2 actively, I don't have the time.

Upgrade PHP ;)

@krakjoe krakjoe closed this as completed Jun 30, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants