- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
最近使用扩展 pthreads ,我发现了一个异常。我有一个带有内部状态的简单对象:
class Sum {
private $value = 0;
public function add($inc) { $this->value += $inc; }
public function getValue() { return $this->value; }
}
现在我创建了一个 Thread 类来处理这个对象:
class MyThread extends Thread {
private $sum;
public function __construct(Sum $sum) {
$this->sum = $sum;
}
public function run(){
for ($i=0; $i < 10; $i++) {
$this->sum->add(5);
echo $this->sum->getValue() . " ";
}
}
}
在我的主函数中,我创建了一个 Sum 对象,将其注入(inject)线程并启动它:
$sum = new Sum();
$thread = new MyThread($sum);
$thread->start();
$thread->join();
echo $sum->getValue();
我预计结果为 50
,因为线程必须将值递增 10 次,每次递增 5。但我得到了 0
!
更奇怪的是,失败的不是同步回主线程,而是线程似乎在途中忘记了它的内部状态:run()
方法中的 echo 输出不是预期的 5 10 15 20 25 30 35 40 45 50
,而是 0 0 0 0 0 0 0 0 0 0
。没有人干扰线程 - 为什么它不保留其状态?
旁注:如果我不启动线程而是直接在主线程中调用 run() 方法 ($thread->run();
),结果仍然是相同的。但是,如果我现在删除类声明中的 extends Thread
,它会完美运行并返回预期的 5 10 15 20 25 30 35 40 45 50
。
最佳答案
任何不是从 pthreads 定义派生的对象都将在将其设置为从 pthreads 派生的对象的成员时序列化。
像 += 和 [] 这样的操作在内部使用指针,序列化与其他对象的指针不兼容。在介绍页面的手册中,它指出任何打算由多个上下文操作的对象都应该扩展 Stackable、Thread 或 Worker,例如
<?php
class Sum extends Stackable {
private $value = 0;
public function add($inc) { $this->value += $inc; }
public function getValue() { return $this->value; }
public function run(){}
}
class MyThread extends Thread {
public $sum;
public function __construct(Sum $sum) {
$this->sum = $sum;
}
public function run(){
for ($i=0; $i < 10; $i++) {
$this->sum->add(5);
echo $this->sum->getValue() . " ";
}
}
}
$sum = new Sum();
$thread = new MyThread($sum);
$thread->start();
$thread->join();
echo $sum->getValue();
?>
如果 Sum 不使用指针,您可以选择在连接后从线程对象中检索引用。
这些都是简单的操作,不需要同步。唯一应该同步的时间是当您计划等待一个对象或通知一个对象时。
与 pthreads 捆绑在一起的对象非常适合这种环境,并且永远不会被序列化。
请务必阅读手册中的介绍和您希望使用的方法中的所有示例,以准确了解是什么,然后随时询问原因:)
我知道 PHP 用户不习惯做研究,但我们在这里挑战极限,您会发现有正确的方法和不正确的方法,其中大部分都记录在示例中,并且任何我不确定的东西都会在 SO 上从我这里提取出来,并最终找到文档的方式。
我不确定你给出的例子是否特别测试对象,但你提供的代码不需要是两个对象,也不应该是两个对象,请考虑以下几点:
<?php
class MyThread extends Thread {
public $sum;
public function run(){
for ($i=0; $i < 10; $i++) {
$this->add(5);
printf("%d ", $this->sum);
}
}
public function add($num) { $this->sum += $num; }
public function getValue() { return $this->sum; }
}
$thread = new MyThread();
$thread->start();
$thread->join();
var_dump($thread->getValue());
?>
如果您看到更多的功能并附上解释,可能会对您有所帮助,所以这里有一个与您的示例类似的示例:
<?php
class MyThread extends Thread {
public $sum;
public function __construct() {
$this->sum = 0;
}
public function run(){
for ($i=0; $i < 10; $i++) {
$this->add(5);
$this->writeOut("[%d]: %d\n", $i, $this->sum);
}
$this->synchronized(function($thread){
$thread->writeOut("Sending notification to Process from %s #%lu ...\n", __CLASS__, $thread->getThreadId());
$thread->notify();
}, $this);
}
public function add($num) { $this->sum += $num; }
public function getValue() { return $this->sum; }
/* when two threads attempt to write standard output the output will be jumbled */
/* this is a good use of protecting a method so that
only one context can write stdout and you can make sense of the output */
protected function writeOut($format, $args = null) {
$args = func_get_args();
if ($args) {
vprintf(array_shift($args), $args);
}
}
}
$thread = new MyThread();
$thread->start();
/* so this is synchronization, rather than joining, which requires an actual join of the underlying thread */
/* you can wait for notification that the thread is done what you started it to do */
/* in these simple tests the time difference may not be apparent, but in real complex objects from */
/* contexts populated with more than 1 object having executed many instructions the difference may become very real */
$thread->synchronized(function($thread){
if ($thread->getValue()!=50) {
$thread->writeOut("Waiting for thread ...\n");
/* you should only ever wait _for_ something */
$thread->wait();
$thread->writeOut("Process recieved notification from Thread ...\n");
}
}, $thread);
var_dump($thread->getValue());
?>
这在一些简单的示例中结合了一些更高级的功能,并进行了注释以帮助您。关于共享对象的主题,如果 Thread 对象包含其他线程或可堆叠对象所需的某些功能和数据,则传递 Thread 对象并没有错。您的目标应该是使用尽可能少的线程和对象来完成工作。
关于php - pthread 线程对象重置它们的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14564234/
我有一个“设置首选项”屏幕。它有一个 ListPreference 和一个 CheckBoxPreference。当我选择 ListPreference 的一项时,我想更改应用程序的日期格式。另外,通
我试图找到创 build 置/配置窗口的示例。单击菜单项中的“选项”操作可启动设置窗口。我想弄清楚如何从主窗口打开第二个窗口。以及新窗口如何将设置信息返回主窗口。尝试使用 QDialog 或一些继承的
我在 Lnux 上有 Qt 应用程序。我想为此创建一个可执行文件/设置以便在 Windows 上分发它并且不需要安装 Qt。我通过包含所有 dll 为此创建了可执行文件但要运行它,用户需要进入文件夹。
我正在尝试创建一个有点动态的 html 类,它根据类末尾包含的数字设置宽度 %。注意:类名将始终以“gallery-item-”开头 示例:div.gallery-item-20 = 20% 宽度 我
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
在我的应用程序中,我想记住一些变量,例如,如果用户登录过一次,那么他们将在下次重新打开应用程序时登录,或者如果他们决定禁用某些提醒,应用程序可以检查该变量是否是错误的,将不再显示该提醒。理想情况下,这
我在 Netbeans 中开发了一个应用程序,它连接到远程计算机的消息队列并发送消息。该应用程序还有其他功能。项目完成后,我清理并构建应用程序,然后 Netbeans 创建一个 jar 文件。 但我的
我创建了一个 Outlook 加载项,需要创建一个设置以使其可分发(我是新手,所以请原谅新手评论) Outlook -2010 Vs -2010 .Net 4.0 我读了一些地方,最简单的方法就是发
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: How to make installer pack of Java swing Application Proje
这个问题肯定已经被很多人解决过很多次了,但是经过几个小时的研究,我仍然没有找到我要找的东西。 我有一个 ExportSettings.settings 文件,其中包含一堆设置( bool 值、字符串、
我想为我的项目创建一个安装程序,以便它可以安装在任何电脑上而无需安装头文件。我怎样才能做到这一点? 最佳答案 一般有两种分发程序的方法: 源代码分发(要构建的源代码)。最常见的方法是使用 GNU au
如何在这样的动态壁纸中创 build 置 Activity ? Example Picture 我只用一个简单的文本构建了设置 Activity ,但遇到了一些问题。第一个问题是我不能为此 Activ
我用 GUI 创建了一个简单的软件。它有几个源文件。我可以在我的编辑器中运行该项目。我认为它已经为 1.0 版本做好了准备。但我不知道如何为我的软件创 build 置/安装程序。 源代码是python
我的 SettingsActivity当前扩展了 Android Studio 生成的类,AppCompatPreferenceActivity扩展 PreferenceActivity . Acti
我正在使用 .NET 为 IE 开发工具栏。目前,我使用 gacutil 插入我的 .NET 程序集,并使用 regasm 注册我的 COM 程序集。 我想为项目创建一个设置 (MSI),但我似乎无法
在为设置页面创建 Activity 后,我注意到 if (mCurrentValue !== value) 中的 mCurrentValue !== value 返回警告: Identity equa
我在 Visual Studio 10 中创建了一个项目,该项目使用 Mysql 数据库和 Crystalreports 以及 它。但是我不知道如何进行自动安装 Mysql 和 Crystalrepo
我正在尝试在我的 C# 项目中使用 Sqlite 数据库,并且我在 IDE 中做得很好。我的问题是当我为我的项目制作安装包并安装它时,程序无法访问 sqlite 数据库。我也知道这是因为用户没有访问文
我有一个大型 Web 应用程序(带有 11 子系统的 ErP),我想使用 Microsoft WebPI 为它创建一个设置。 目前,我们每周向客户发送一次应用程序(用于每周更新)。 我们在此应用程序中
所以我对工资单申请的最终查询是 - 如何为薪资申请创 build 置? 我需要知道的一切- 如何将设置项目添加到我现有的解决方案 如何将解决方案中的文件添加到安装项目中,以及添加哪些文件添加和在什么文
我是一名优秀的程序员,十分优秀!