如何使用 pthread 访问数组变量,我创建了一个线程类名称“AccessVariable”,其任务是创建 4 个线程并访问名为“$arr”的数组,需要一些关于如何实现这一点的指针,因为我非常此编码中的新内容
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
class AccessVariable extends Thread {
public $arr = array("12","33","21","3211","3214","34","23423");
public function __construct($arg) {
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$tmp_value = $this->getValue();
printf('%s: %s %d -finish' . "\n", date("g:i:sa"), $this->arg, $tmp_value);
}
$this->synchronized(function($thread) {
$thread->getValue();
}, $this);
}
public function getValue() {
//Get Array Variable
}
}
// Create a array
$stack = array();
//Iniciate Miltiple Thread
foreach (range("A", "D") as $i) {
$stack[] = new AccessVariable($i);
}
// Start The Threads
foreach ($stack as $t) {
$t->start();
}
?>