JavaScript中的重入 [英] Reentrancy in JavaScript

查看:115
本文介绍了JavaScript中的重入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提高对折旧这个词的理解。

I would like to improve my understanding of the word reentrant.

这个函数是否可以重入?

Is this function reentrant?

function* foo() {
  yield 1;
  yield 2;
}

这一个?

function foo() {
  return 1;
}

这一个?

var x = 0;
function foo() {
  return x++;
}

这一个?

function foo() {
  setTimeout(foo, 1000);
}


推荐答案

reentrent函数是一个函数其执行可以恢复:

A reentrent function is a function whose execution can be resumed:


在计算中,如果计算机程序或子程序在执行过程中被中断,则称其为可重入的然后在其先前的调用完成执行之前再次安全地调用(重新输入)。

In computing, a computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution.

在浏览器/节点JavaScript中,所有多处理都是协作的(没有中断或上下文切换)。 常规函数总是在JavaScript中运行完毕。(1)

In browser/node JavaScript, all multiprocessing is cooperative (no interrupts or context switches). A regular function always runs to completion in JavaScript. (1)

所以在你的情况下 - 唯一的reentrent函数是第一个,因为它不会运行其代码完成,可以在以后恢复。

So in your case - the only reentrent function is the first one since it doesn't run its code to completion and can be resumed at a later point.


  • 第二个函数只是一个常规函数。

  • 第三个使用外部范围,它有点类似,因为它允许函数保持某种状态。这不是一回事,因为函数无法恢复。

  • 第四个只是立即运行完成(它安排了另一个调用 - 但这取决于平台而不是JavaScript)。

  • The second function is just a regular function.
  • The third one uses an outer scope, which is kind of similar because it lets a function hold some state. It's not the same thing though since the function can't be resumed.
  • The fourth one just runs to completion immediately (it schedules another invokation of it - but that's up to the platform and not JavaScript).

确实 - 可以说生成器在JavaScript中使用重复语法启用协作式多任务处理。在生成器之前,所有代码都运行完成

Indeed - one can say that generators enable cooperative multitasking in JavaScript with a reentrent syntax. Before generators all code ran to completion.

(1)或者它永远不会停止,但它永远不会被打断。此外 - 在通用平台。有一些平台(如Rhino)打破了规则。它们非常罕见,并且不使用与浏览器/节点JS相同的并发执行模型。

这篇关于JavaScript中的重入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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