设置 onmessage 处理程序时与网络工作者的竞争条件?
请考虑以下代码以及本 Mozilla 教程“使用 Web Worker” 中的说明:
var myWorker = new Worker('my_worker.js');
myWorker.onmessage = function(event) {
print("Called back by the worker!\n");
};
本例中的第 1 行创建并 开始运行工作线程。 第 2 行设置 onmessage 处理程序 工人的功能是 当worker调用自己的时候被调用 postMessage() 函数。
线程在调用 Worker 构造函数时启动。我想知道设置 onmessage 处理程序时是否可能存在竞争条件。例如,如果网络工作人员在设置 onmessage 之前发布一条消息。
有人对此了解更多吗?
更新:
Andrey 指出,网络工作者应该在收到消息时开始工作,就像 Mozilla 教程中的斐波那契示例一样。但这是否会在 Web Worker 中设置 onmessage 处理程序时产生新的竞争条件?
例如:
主脚本:
var myWorker = new Worker('worker.js');
myWorker.onmessage = function(evt) {..};
myWorker.postMessage('start');
Web Worker 脚本('worker.js')
var result = [];
onmessage = function(evt) {..};
然后考虑以下执行路径:
main thread web worker
var worker = new Worker("worker.js");
var result = [];
myWorker.onmessage = ..
myWorker.postMessage('start');
onmessage = ..
“var result = []”行可以省略,它仍然会可以达到同样的效果。
这是一个有效的执行路径,我通过在网络工作者中设置超时来尝试它!目前我不明白如何使用网络工作者而不遇到竞争条件?!
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?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案是主脚本和 Web Worker 都有一个 MessagePort 队列,用于在初始 Worker 脚本返回之前收集消息。
有关详细信息,请参阅 WHATWG 帮助邮件列表上的此主题:
https://web.archive.org/web/20140401202427/http://lists.whatwg.org/pipermail/help-whatwg.org/2010-August/000606.html
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:
https://web.archive.org/web/20140401202427/http://lists.whatwg.org/pipermail/help-whatwg.org/2010-August/000606.html