0

I'm struggling to find the problem with the following piece of PHP code:

The code is as follows

class WorkerThreads extends Thread
{
  private $from_list;

  public function __construct($x,$host,$users_email,$pass,$inbox)
  {
    $this->from_list = array(); # holds the unique froms extracted from headers
  }

  public function run()
  {
    # Get Froms
    if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
      $from = trim(str_ireplace("From: ", "", $matches[0][0]));
      if (!array_key_exists($from, $this->from_list)) {
        $this->from_list[$from] = 1;
        echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
      }
    } else {
      echo "NO FROM <br/><rb/>";
    }

The following error occurs:

Notice: Undefined index: Viva in /var/www/BAMCode/yahoofroms.php on line 198    
FROM: Viva-

The offending line 200 is

echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";

There seems to be a problem with the array

Del
  • 131
  • 2
  • 13
  • So find the line number 198 and check what variable contains the string `Viva` ? – adeneo Jun 10 '15 at 02:11
  • This is line 198 echo "
    FROM: ".$from."-".$this->from_list[$from]."

    "; The $from contains Viva - the array for some reason is generating the error...
    – Del Jun 10 '15 at 02:13
  • So `$this->from_list` doesn't have anything with the key `Viva` then – adeneo Jun 10 '15 at 02:18
  • Have you initialized `$this->from_list` as array before assigning `$this->from_list[$from]` ? – oscargilfc Jun 10 '15 at 02:19
  • Yes - in an earlier function - $this->from_list = array(); – Del Jun 10 '15 at 02:21
  • If you created it within a function, it won't be available outside the function. `printr` the array above the line throwing the error – Anthony Jun 10 '15 at 02:30
  • I've added the code above where it was created - it wasn't created within the function.When I try printing it out, it throws an error... – Del Jun 10 '15 at 02:36
  • Dagon, This isn't a duplicate of that qu - the variable is defined - there is something else going on which relates to multiple threads... – Del Jun 10 '15 at 03:18

1 Answers1

-1

This will work perfectly fine if you don't use multiple threads.

I think many threads are trying to modify and read the same list. Try synchronizing the modification/read of the from_list. We may use a mutex to synchronize read/write to the from_list.

Check the code below:

<?php
class WorkerThreads extends Thread
{
  private $from_list;
  private $mylock;

  public function __construct($x,$host,$users_email,$pass,$inbox)
  {
    $this->from_list = array(); # holds the unique froms extracted from headers
    $this->mylock = Mutex::create();
  }

  public function __destruct()
  {
    Mutex::destroy($this->mylock);
    $this->mylock = null;
  }

  public function run()
  {
    # Get Froms
    if (preg_match('/From\:\ (.+)/i', $headers, $matches, PREG_OFFSET_CAPTURE)) {
      $from = trim(str_ireplace("From: ", "", $matches[0][0]));
      Mutex::lock($this->mylock);
      if (!array_key_exists($from, $this->from_list)) {
        $this->from_list[$from] = 1;
        echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
      }
      Mutex::unlock($this->mylock);
    } else {
      echo "NO FROM <br/><rb/>";
    }
  }
}

?>
Nipun Talukdar
  • 4,975
  • 6
  • 30
  • 42
  • How would I do that? I'm a little unclear on threading tbh. – Del Jun 10 '15 at 02:55
  • @Del I have updated the answer. You may use a mutex for synchronizing critical sections. Or you may use synchronized block as well. – Nipun Talukdar Jun 10 '15 at 04:47
  • I still get the same error nipun as wel as this at the end "Notice: Undefined property: WorkerThreads::$mylock on line 162" where line162 is Mutex::destroy($this->mylock); – Del Jun 11 '15 at 21:35
  • Each new class has it's own set of private vars - I'm not sure we need to use mutex as the various classes are not sharing vars – Del Jun 11 '15 at 22:07
  • Add mylock as a private variable. Check the modified code. Can you paste the entire program somewhere, may be in ideone.com? Then only it will be clear where the error is. – Nipun Talukdar Jun 12 '15 at 01:53
  • Here's the entire code Nipun http://ideone.com/RpfWIp - the problem seems to be the use of arrays in php pthreads... it appears they have to be managed in a different way - locking isn't the issue. – Del Jun 12 '15 at 13:39