0

I have the following code where I'm attempting to assign a value to an array in a class that uses php pthreads to no avail - I have looked at solutions that suggest using stacks to no avail either:

<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);

    class WorkerThreads extends Thread
        {
            private $fromlist;

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

            public function run()
            {
                   $this->fromlist=array("hello"=>1,2);
                   $this->fromlist['hi']="!!!";
                   $this->fromlist[] = array("ho", 1);
            }
        }

            $workers = new WorkerThreads();
            $workers->start();
            print_r($workers);

I get the following results:

WorkerThreads Object
(
    [fromlist] => Array
        (
            [hello] => 1
            [0] => 2
        )

)

I was expecting to see additional array elements - try as I may I cannot get it to add more elements.

I've checked the syntax and it seems fine - the problem appears to be how to use an array in pthreads - I can't seem to make sense of how to do that.

Can anyone tell me what I'm doing wrong?

Or suggest some code for the above so I can get to a working solution?

4
  • Why are you doing private $fromlist = array(); inside of your __construct() method...? Commented Jun 12, 2015 at 1:59
  • Typo.. whilst debugging I forgot to remove that... It still does not work - I think arrays may not be serializable in multithreading... Commented Jun 12, 2015 at 2:52
  • 1
    possible duplicate of A PHP/pthreads Thread class can't use array? Commented Jun 12, 2015 at 6:11
  • Nope - not duplicate of that question Joe as that expects same space arrays - my code does not. I've posted the answer below which does not appear on that q&a. Commented Jun 12, 2015 at 22:00

1 Answer 1

1

Adding the $fromlist variable to the run() function solves the problem.

Sign up to request clarification or add additional context in comments.

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.