什么是“thunk”?

发布于 2024-08-28 17:53:46 字数 78 浏览 4 评论 0原文

我见过它在编程中使用(特别是在 C++ 领域),但不知道它是什么。据推测这是一种设计模式,但我可能是错的。谁能举一个 thunk 的好例子吗?

I've seen it used in programming (specifically in the C++ domain) and have no idea what it is. Presumably it is a design pattern, but I could be wrong. Can anyone give a good example of a thunk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

不必在意 2024-09-04 17:53:46

thunk 通常是指作为函数调用的一小段代码,做一些小事情,然后跳转到另一个位置(通常是函数)而不是返回到它的调用者。假设JUMP目标是一个普通函数,当它返回时,它将返回到thunk的调用者。

thunk 可用于高效地实现许多有用的东西

  • 协议转换 - 当从使用一种调用约定的代码调用到使用不同调用约定的代码时,thunk 可用于转换论据适当。仅当返回约定兼容时才有效,但通常情况就是这样

  • 虚拟函数处理通常就是这种情况——在 C++ 中调用多重继承基类的虚拟函数时,需要修复 < code>this 指针使其指向正确的位置。 thunk 可以做到这一点。

  • 动态闭包——当您构建动态闭包时,闭包函数需要能够获取创建它的上下文。可以构建一个小的thunk(通常在堆栈上),它在某些寄存器中设置上下文信息,然后跳转到实现闭包功能的静态代码段。这里的 thunk 有效地向函数提供一个或多个调用站点未提供的隐藏额外参数。

A thunk usually refers to a small piece of code that is called as a function, does some small thing, and then JUMPs to another location (usually a function) instead of returning to its caller. Assuming the JUMP target is a normal function, when it returns, it will return to the thunk's caller.

Thunks can be used to implement lots of useful things efficiently

  • protocol translation -- when calling from code that uses one calling convention to code that uses a different calling convention, a thunk can be used to translate the arguments appropriately. This only works if the return conventions are compatible, but that is often the case

  • virtual function handling -- when calling a virtual function of a multiply-inherited base class in C++, there needs to be a fix-up of the this pointer to get it to point to the right place. A thunk can do this.

  • dynamic closures -- when you build a dynamic closure, the closure function needs to be able to get at the context where it was created. A small thunk can be built (usually on the stack) which sets up the context info in some register(s) and then jumps to a static piece of code that implements the closure's function. The thunk here is effectively supplying one or more hidden extra arguments to the function that are not provided by the call site.

ζ澈沫 2024-09-04 17:53:46

thunk 这个词在计算机科学中至少有三个相关的含义。 “thunk”可能是:

  • 执行延迟的一段代码
    计算(类似于闭包)
  • 某些虚函数的功能
    表实现(类似于
    包装函数)
  • 机器数据的映射
    系统特定的形式到另一个,
    通常出于兼容性原因,

我通常看到它在第三种情况下使用。

http://en.wikipedia.org/wiki/Thunk

The word thunk has at least three related meanings in computer science. A "thunk" may be:

  • a piece of code to perform a delayed
    computation (similar to a closure)
  • a feature of some virtual function
    table implementations (similar to a
    wrapper function)
  • a mapping of machine data from one
    system-specific form to another,
    usually for compatibility reasons

I have usually seen it used in the third context.

http://en.wikipedia.org/wiki/Thunk

何以心动 2024-09-04 17:53:46

thunk 一词最初是指 皇家雷达建立 实现名称传递所使用的机制在他们的 Algol60 编译器中。一般来说,它指的是在引用明显静态的对象时引发动态行为的任何方式。这个术语是由 Brian Wichmann 发明的,当他被要求解释按名称传递时,他说:“好吧,你出去从内存中加载值,然后突然——砰——你正在评估一个表达式。”

Thunk 已被放入硬件中(参见 KDF9、Burroughs 大型机)。有多种方法可以在软件中实现它们,所有方法都特定于机器、语言和编译器。

该术语已被广泛推广,超越了名称传递的范围,包括任何表面上或名义上的静态数据引用引起动态行为的情况。相关术语包括“蹦床”和“未来”。

The term thunk originally referred to the mechanism used by the Royal Radar Establishment implementation of pass-by-name in their Algol60 compiler. In general it refers to any way to induce dynamic behavior when referencing an apparently static object. The term was invented by Brian Wichmann, who when asked to explain pass-by-name said "Well you go out to load the value from memory and then suddenly - thunk - there you are evaluating an expression."

Thunks have been put in hardware (cf. KDF9, Burroughs mainframes). There are several ways to implement them in software, all very machine, language and compiler specific.

The term has come to be generalized beyond pass-by-name, to include any situation in which an apparently or nominally static data reference induces dynamic behavior. Related terms include "trampoline" and "future".

思念绕指尖 2024-09-04 17:53:46

一些面向对象语言(例如 C++)的编译器生成称为“thunk”的函数,作为存在多重继承或虚拟继承时虚拟函数调用的优化。

取自:http://en.wikipedia.org/wiki/Thunk#Thunks_in_object-oriented_programming

Some compilers for object-oriented languages such as C++ generate functions called "thunks" as an optimization of virtual function calls in the presence of multiple or virtual inheritance.

Taken from: http://en.wikipedia.org/wiki/Thunk#Thunks_in_object-oriented_programming

美胚控场 2024-09-04 17:53:46

使用上有很大差异。几乎普遍地,thunk 是一个(至少在概念上)异常小和简单的函数。它通常是某种适配器,为您提供与某些事物或其他事物(某些数据、另一个函数等)的正确接口,但至少被视为几乎没有做其他事情。

它几乎就像语法糖的一种形式,只不过(至少像通常使用的那样)语法糖应该使事物看起来像人类读者希望看到的那样,而 thunk 是使事物看起来像编译器想要的那样看到它。

There's considerable variation in use. Almost universally, a thunk is a function that's (at least conceptually) unusually small and simple. It's usually some sort of adapter that gives you the correct interface to something or other (some data, another function, etc.) but is at least seen as doing little else.

It's almost like a form of syntactic sugar, except that (at least as usually used) syntactic sugar is supposed to make things look the way the human reader wants to see them, and a thunk is to make something look the way the compiler wants to see it.

×纯※雪 2024-09-04 17:53:46

这个问题已经被问过,请参阅:

什么是“thunk”,如Scheme或一般情况下使用的那样?

据我所知,它类似于lambda语句,在其中您可能不想返回值,直到需要评估它;或者它也可以与属性 getter 进行比较,属性 getter 根据设计执行一些代码以返回值,同时具有更像变量的接口形式,而且还具有可以通过继承或交换的多态行为通过交换函数指针,该指针将根据编译时或环境特征在运行时评估并返回一个值。

This question has already been asked on SO, see:

What is a 'thunk', as used in Scheme or in general?

From what I can tell, it's akin to a lambda statement, where you may not want to return the value until you need to evaluate it; or it can also be compared to a property getter which by design executes some code in order to return a value while yet having the interface form that comes across more like a variable, but also has polymorphic behavior that can be swapped out whether by inheritance or by swapping out the function pointer that would evaluate and return a value at runtime based on compile-time or environmental characteristics.

晨敛清荷 2024-09-04 17:53:46

我很苦恼地发现这个术语没有通用的“计算机科学”定义与其历史上已知的实际用法相匹配。我记得第一次在现实生活中遇到它实际上是在 OS/2 天和 16-32 位过渡期间。看来“thunking”在今天的应用中就像是讽刺。

我粗略的一般理解是,thunk 是一个存根例程,它什么也不做,或者像前面提到的历史案例一样跨越系统之间的一些基本边界。

因此,这种感觉就像从一个环境到另一个环境的联觉,发出(隐喻/作为明喻)“重击”的声音。

I was distressed to find no general 'computer science' definition of this term matching its de-facto usage as known historically to me. The first real-life encounter I can recall where it was actually called that was in the OS/2 days and the 16-32 bit transition. It appears "thunking" is like irony in its application today.

My rough general understanding is that the thunk is a stub routine that just does nothing or routes across some fundamental boundary in kind between systems as in the mentioned historical cases.

So the sense is like a synesthesia of being dropped from the one environment to the other making (metaphorically/as a simile) a "thunk" sound.

清欢 2024-09-04 17:53:46

我打算查一下,但我认为 thunking 是 32 位处理器用来运行旧版 16 位代码的过程。

我曾经用它来比喻,当你与哑巴交谈时,你必须限制你说话的速度和使用的词语。

是的,它在维基百科链接中(关于 32 位的部分,不是我的书呆子)。

https://en.wikipedia.org/wiki/Thunk

许多关于互操作性 thunk 的文献涉及各种 Wintel 平台,包括 MS-DOS、OS/2、[8]Windows[9][10] 和 .NET,以及从 16 位的过渡到 32 位内存寻址。随着客户从一个平台迁移到另一个平台,thunk 对于支持为旧平台编写的遗留软件至关重要。

(重点是我加上的)

I'm going to look this up, but I thought thunking was the process employed by a 32-bit processor to run legacy 16-bit code.

I used to use it as an analogy for how you have to restrict how fast you talk and what words you use when talking to dumb people.

Yeah, it's in the Wikipedia link (the part about 32-bit, not my nerdalogy).

https://en.wikipedia.org/wiki/Thunk

Much of the literature on interoperability thunks relates to various Wintel platforms, including MS-DOS, OS/2,[8]Windows[9][10] and .NET, and to the transition from 16-bit to 32-bit memory addressing. As customers have migrated from one platform to another, thunks have been essential to support legacy software written for the older platforms.

(emphasis added by me)

拥抱影子 2024-09-04 17:53:46

据我所知,最早使用“thunk”是从 50 年代末开始,参考函数调用中的 Algol60 按名称传递参数求值。 Algol 最初是一种规范语言,而不是一种编程语言,并且存在一些关于如何在计算机上实现按名称传递的问题。

解决方案是传递本质上是 lambda 的入口点。当被调用者评估参数时,控制失败了 - 砰! - 进入调用者的上下文,其中 lambda 被求值,其结果成为被调用者中参数的值。

在带标签的硬件中,例如 Burroughs 机器,评估是隐式的:参数可以作为数据值传递,就像普通的按值传递一样,或者通过 thunk 来传递名称,并在参数元数据中使用不同的标签。加载操作硬件检查标签并返回简单值或自动调用 lambda thunk。

The earliest use of "thunk" I know of is from late '50s in reference to Algol60 pass-by-name argument evaluation in function calls. Algol was originally a specification language, not a programming language, and there was some question about how pass-by-name could be implemented on a computer.

The solution was to pass the entry point of what was essentially a lambda. When the callee evaluated the parameter, control fell through - thunk! - into the caller's context where the lambda was evaluated and it's result became the value of the parameter in the callee.

In tagged hardware, such as the Burroughs machines, the evaluation was implicit: an argument could be passed as a data value as in ordinary pass-by-value, or by thunk for pass-by-name, with different tags in the argument metadata. A load operation hardware checked the tag and either returned the simple value or automatically invoked the lambda thunk.

浊酒尽余欢 2024-09-04 17:53:46

根据 Kyle Simpson 的定义,thunk 是一种从异步代码中抽象出时间 组件的方法。

Per Kyle Simpson's definition, a thunk is a way to abstract the component of time out of asynchronous code.

狼性发作 2024-09-04 17:53:46

《新黑客词典》的早期版本声称 thunk 是一个不带参数的函数,并且它是针对一个特别棘手的问题的一个简单的深夜解决方案,其中“thunk”是假定的过去“想”的时态,因为他们早就该想到了。

An earlier version of The New Hacker's Dictionary claimed a thunk is a function that takes no arguments, and that it was a simple late-night solution to a particularly tricky problem, with "thunk" being the supposed past tense of "think", because they ought to have thunk of it long time ago.

北凤男飞 2024-09-04 17:53:46

在 OCaml 中,它是一个以单位“()”作为参数的函数(不带参数,通常用于副作用)

In OCaml, it’s a function which takes unit “()” as a param (takes no params, often used for side effects)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文