我正在努力通过以下PHP代码找到问题:
代码如下
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/>";
}
发生以下错误:
Notice: Undefined index: Viva in /var/www/BAMCode/yahoofroms.php on line 198
FROM: Viva-
违规行200是
echo "<br/>FROM: ".$from."-".$this->from_list[$from]."<br/><br/>";
阵列似乎有问题
答案 0 :(得分:-1)
如果你不使用多个线程,这将完全正常。
我认为很多线程都试图修改和读取相同的列表。尝试同步from_list的修改/读取。我们可以使用互斥锁来同步读/写到from_list。
检查以下代码:
<?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/>";
}
}
}
?>