Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a 'thunk'?

Tags:

c++

thunk

People also ask

What is a thunk in computer science?

A thunk, in a general computing context, is a piece of low-level machine generated code that implements the details of a software system. It is one of the following: A piece of code performing a delayed computation. A feature of a virtual function table implementation.

What is a thunk in react?

Thunk is a programming concept where a function is used to delay the evaluation/calculation of an operation. Redux Thunk is a middleware that lets you call action creators that return a function instead of an action object.

What does thunk stand for?

(Entry 1 of 3) dialectal past tense and past participle of think.

What is a thunk task?

What is a "thunk"? ​ The word "thunk" is a programming term that means "a piece of code that does some delayed work". Rather than execute some logic now, we can write a function body or code that can be used to perform the work later.


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.


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


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


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