php多线程访问数组变量 [英] php mutilthreading accessing a array variables
问题描述
如何使用 pthread 访问数组变量,我创建了一个线程类名称 "AccessVariable",其任务是创建 4 个线程并访问名为 "$arr",需要一些关于如何实现这一点的指针,因为我对这个编码很陌生
How to access a array variables using pthread, i have created a thread class names "AccessVariable", whoose task is to create 4 thread and access the array named "$arr", need some pointer on how to acieve this as i 'm very new in this coding
<?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();
}
?>
推荐答案
一些您会发现有用的观察:
Some observations you will find useful:
- 不支持类条目默认值 - zend 有对象处理程序但没有条目处理程序,当一个条目被声明时,对象处理程序显然不适合调用,因为它们与实例一起工作.要解决此问题,请在构造函数中设置默认值.
- 用于在多个上下文之间共享的变量应该扩展 pthreads 定义;pthreads 对象作为对象运行,并关联数组和索引列表,这些东西的默认 PHP 实现没有为多线程做好准备.
- 同步对象仅在您打算在同步块中使用同步方法时才有用(php 中的闭包/函数);仅在您打算等待时进行同步,或通知其他等待和反对的人
-
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
class SharedArray extends Stackable {
public function __construct($array) {
$this->merge($array);
}
public function run(){}
}
class AccessSharedArray extends Thread {
public $shared;
public $arg;
public function __construct($shared, $arg) {
$this->shared = $shared;
$this->arg = $arg;
}
public function run() {
if ($this->arg) {
$tmp_value = $this->shared[$this->arg];
printf('%s: %s %d -finish' . "\n", date("g:i:sa"), $this->arg, $tmp_value);
}
}
}
$shared = new SharedArray(
array("12","33","21","3211","3214","34","23423"));
// Create a array
$stack = array();
foreach (range(0, 3) as $i) {
$stack[] = new AccessSharedArray($shared, $i);
}
// Start The Threads
foreach ($stack as $t) {
$t->start();
}
foreach ($stack as $t)
$t->join();
?>
github 上有很多示例并包含在发行版中,以帮助您充分了解 pthread 以使用它.多线程不像使用新的数据库或 http 客户端.它既复杂又强大,我恳请您仔细阅读包含的每个示例,即使您认为它与手头的任务无关;无论如何,这些知识都会为您服务.
There are many examples on github and included in the distribution to help you to get to know pthreads well enough to use it. Multi-threading is not like using a new database, or http client. It is complex and powerful, I implore you to read carefully and fully every example included even if you think it is irrelevant to the task at hand; the knowledge will serve you well whatever.
除了示例之外,github上过去的错误报告中有很多信息,因此如果您遇到问题并且示例中似乎没有解决方案,请在报告任何错误之前搜索github上的问题列表.
In addition to examples, there is much info in past bug reports on github, so if you have a problem and there doesn't seem to be a solution in an example then search the issues list on github before reporting any bugs.
这篇关于php多线程访问数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!