What is a 'thunk'?

C++Thunk

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Solution 3 - C++

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".

Solution 4 - C++

>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

Solution 5 - C++

This question has already been asked on SO, see:

https://stackoverflow.com/questions/925365/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.

Solution 6 - C++

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.

Solution 7 - C++

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)

Solution 8 - C++

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.

Solution 9 - C++

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.

Solution 10 - C++

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

Solution 11 - C++

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.

Solution 12 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfbreretoView Question on Stackoverflow
Solution 1 - C++Chris DoddView Answer on Stackoverflow
Solution 2 - C++Robert HarveyView Answer on Stackoverflow
Solution 3 - C++Ivan GodardView Answer on Stackoverflow
Solution 4 - C++OscarRyzView Answer on Stackoverflow
Solution 5 - C++Jon DavisView Answer on Stackoverflow
Solution 6 - C++Jerry CoffinView Answer on Stackoverflow
Solution 7 - C++MusiGenesisView Answer on Stackoverflow
Solution 8 - C++仁 人 卷View Answer on Stackoverflow
Solution 9 - C++Ivan GodardView Answer on Stackoverflow
Solution 10 - C++Alex GordonView Answer on Stackoverflow
Solution 11 - C++user2492477View Answer on Stackoverflow
Solution 12 - C++GL2014View Answer on Stackoverflow