1

I am trying to create a library for codeigniter that uses pthread, everything works fine but when i want to assign a value to an array this don't work with traditional $a['key'] = 'val';

Small Test Exemple : ( updated )

class Test {
    protected $core;
    protected $stack;

    public function init(){
        $this->stack = new Test_Stack();
        $this->core = new Test_Core($this->stack);
    }

    public function do_test(){
        return $this->core->assign();
    }
}


class Test_Stack extends Stackable {
    protected $a;

    function __construct(){
        $this->a = array();
    }

    protected function test(){ // Call from other class extends Threads
        $this->a['key1'] = 'NOWORK';
        print_r($this->a); // THIS RETURN NOTHING   

        $this->a = array_merge($this->a, array('key1' => 'WORK'));
        print_r($this->a); // NOW THIS GOOD RETURN Key1..
    }

    public function run(){}
}

class Test_Core {
    protected $thread;
    protected $stack;

    function __construct($s){
       $this->stack = $s;
    }

    public function assign(){
        $this->thread = new Test_Thread($this->stack);
        $this->thread->start();

        $this->thread->join();
    }
}

class Test_Thread extends Thread{
    protected $stack;

    function __construct($s){
       $this->stack = $s;
    }

    public function run(){
        $this->stack->test();
    }
}

I write this basic code without testing but it's the same structure of my lib and need this to extends test_stack and add or change test function for exemple.

Even if works now, I would understand why I can't assign my array normally ? Or rather, what am I doing wrong?

6
  • Works fine for me! Output which i get: Array ( [key1] => NOWORK ) Array ( [key1] => WORK ) Commented Jan 7, 2015 at 15:21
  • oK it's very strange, i will post new code that is more like mine implementation. Thx Rizier123 Commented Jan 7, 2015 at 15:27
  • Do you have notices enabled? Commented Jan 7, 2015 at 15:28
  • 1
    possible explanation: stackoverflow.com/questions/14796674/… Commented Jan 7, 2015 at 15:28
  • Yes Marek, error_reporting = E_ALL, display_error On and nothing appears Commented Jan 7, 2015 at 15:33

2 Answers 2

0

Working in Mine :

class Stack  {
    protected $a;

    function __construct(){
        $this->a = array();
    }

    function test(){ 
         $this->a['key1'] = 'NOWORK';
         print_r($this->a); // THIS RETURN NOTHING


         $this->a = array_merge($this->a, array('key1' => 'WORK'));
         print_r($this->a); // NOW THIS GOOD RETURN Key1..
    }
}


$obj = new Stack();
$obj->test();

Output :

Array
(
    [key1] => NOWORK
)
Array
(
    [key1] => WORK
)
Sign up to request clarification or add additional context in comments.

1 Comment

Yes Tiger thx but you don't use Pthread and Stackable class :(
0

OK array isn't thread safe, should be used stackable, this do the trick :

Libraries/Test.php :

class Test {
    protected $core;
    protected $stack;

    public function init(){
        $this->stack = new Test_Stack();
        $this->core = new Test_Core($this->stack);
    }

    public function do_test(){
        $this->core->assign();
        $this->core->assign();
        $this->core->assign();
    }

    public function get_a(){
        return $this->core->get_a();
    }
}

class Test_Array_Stack extends Stackable {
    public function run(){}
}

class Test_Stack extends Stackable {
    protected function test($a){
        $a[] = 'WORK';
    }
    public function run(){}
}

class Test_Core {
    protected $thread;
    protected $stack;
    protected $a;

    function __construct($s){
       $this->stack = $s;

       $this->a = new Test_Array_Stack();
    }

    public function assign(){
        $this->thread = new Test_Thread($this->stack, $this->a);
        $this->thread->start();

        $this->thread->synchronized(function($thread){
            $thread->wait();
        }, $this->thread);

        return $this->a;
    }

    public function get_a(){
        return $this->a;
    }
}

class Test_Thread extends Thread{
    protected $stack;
    protected $a;

    function __construct($s, $a){
       $this->stack = $s;
       $this->a = $a;
    }

    public function run(){
        $this->stack->test($this->a);

        $this->synchronized(function($thread){
            $thread->notify();
        }, $this);
    }
}

You can extends Test_Stack to create lib extension more useful for my project, like this on other file Test_Info.php :

require_once APPPATH.'libraries/Test.php';

class Test_Info extends Test {

    function init(){
        $this->stack = new Test_Info_Stack();
        $this->core = new Test_Core($this->stack);
    }
}

class Test_Info_Stack extends Test_Stack {
    protected function test($a){
        parent::test($a);
        $a[] = 'INFO';
    }
}

And usage on controller:

function index(){
    //without extension
    $this->load->library('Test');
    $this->test->init();
    $this->test->do_test();
    print_r($this->test->get_a());

    //with extension
    $this->load->library('Test_Info');
    $this->test_info->init();
    $this->test_info->do_test();
    print_r($this->test_info->get_a());
}

It took me some time, i hope it will help someone and LuckyBurger thank you for the explanation link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.