设置 onmessage 处理程序时与网络工作者的竞争条件? [英] Race-condition with web workers when setting onmessage handler?

查看:30
本文介绍了设置 onmessage 处理程序时与网络工作者的竞争条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码和 Mozilla 教程中的解释使用网络工作者":

var myWorker = new Worker('my_worker.js');myWorker.onmessage = 函数(事件){print("被worker回调了!\n");};

<块引用>

本例中的第 1 行 创建和开始运行工作线程.第 2 行设置 onmessage 处理程序工人到一个功能是当工人调用自己的时调用postMessage() 函数.

线程在 Worker 构造函数被调用的那一刻开始.我想知道在设置 onmessage 处理程序时是否可能存在竞争条件.例如,如果网络工作者在设置 onmessage 之前发布一条消息.

有人对此了解更多吗?

更新:

Andrey 指出 web worker 应该在收到消息时开始工作,就像 Mozilla 教程中的斐波那契示例一样.但这不会在 Web Worker 中设置 onmessage 处理程序时产生新的竞争条件吗?

例如:

主脚本:

var myWorker = new Worker('worker.js');myWorker.onmessage = function(evt) {..};myWorker.postMessage('开始');

网络工作者脚本('worker.js')

var 结果 = [];onmessage = function(evt) {..};

然后考虑如下执行路径:

主线程web workervar worker = new Worker("worker.js");var 结果 = [];myWorker.onmessage = ..myWorker.postMessage('开始');消息 = ..

var result = []"这一行可以省略,效果还是一样的.

这是一个有效的执行路径,我通过在 web worker 中设置超时来尝试它!目前我看不到,如何在不遇到竞争条件的情况下使用网络工作者?!

解决方案

答案是主脚本和 web worker 都有一个 MessagePort 队列,它在 initial工作脚本返回.

有关详细信息,请参阅 WHATWG 帮助邮件列表上的此主题:http://lists.whatwg.org/pipermail/help-whatwg.org/2010-August/000606.html

Please consider the following code and the explanation from this Mozilla tutorial "Using web workers":

var myWorker = new Worker('my_worker.js');
myWorker.onmessage = function(event) {
  print("Called back by the worker!\n");
};

Line 1 in this example creates and starts running the worker thread. Line 2 sets the onmessage handler for the worker to a function that is called when the worker calls its own postMessage() function.

The thread is started in the moment the Worker constructor is called. I wonder if there might be a race-condition on setting the onmessage handler. For example if the web worker posts a message before onmessage is set.

Does someone know more about this?

Update:

Andrey pointed out that the web worker should start its work, when it receives a message, like in the Fibonacci example in the Mozilla tutorial. But doesn't that create a new race-condition on setting the onmessage handler in the web worker?

For example:

The main script:

var myWorker = new Worker('worker.js');
myWorker.onmessage = function(evt) {..};
myWorker.postMessage('start');

The web worker script ('worker.js')

var result = [];
onmessage = function(evt) {..};

And then consider the following execution path:

main thread                                  web worker
var worker = new Worker("worker.js");
                                             var result = [];
myWorker.onmessage = ..
myWorker.postMessage('start');
                                             onmessage = ..

The "var result = []" line can be left out, it will still be the same effect.

And this is a valid execution path, I tried it out by setting a timeout in the web worker! At the moment I can not see, how to use web workers without running into race-conditions?!

解决方案

The answer is that both the main script and the web worker have a MessagePort queue which collects the messages before the initial worker script returns.

For details, see this thread on the WHATWG help mailing list: http://lists.whatwg.org/pipermail/help-whatwg.org/2010-August/000606.html

这篇关于设置 onmessage 处理程序时与网络工作者的竞争条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆