What Does Thunk Mean?
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
- A mapping of machine data from one system specific form to another for compatibility issues
Techopedia Explains Thunk
The concepts of thunk emerged in early 1961. It is a computation returning a value of the argument when executed. A call by need replaces a thunk by its return value after the first execution. Languages with late binding have lookup in run time, based on computations performed by thunk.
Nullary functions (functions which do not take arguments) in functional programming are referred to as thunk. Thunks simulate lazy evaluation and delay the function argument computation. These functions further force thunks to get actual values. Thunk may also appear naturally in the implementation of constant functions in high order programming.
Compilers of object oriented languages such as C++ generate functions termed thunks. They optimize virtual function calls in the presence of virtual or multiple inheritances.
Some instances of relocatable code use local thunks to call library functions. Dynamic library call in the code jumps to thunks in a jump table, which is further replaced by functions loading the applicable library or moves to a suitable point in an already loaded library. So a thunk in this area computes or returns previously computed and cached values. Thunks are used in software based virtual memory systems to perform a mapping from virtual to physical address. Creating a 16-bit virtual DOS machine inside a 32-bit OS is called thunking and ensures a backward compatibility with applications using old system calls.
Flat thunks use thunks translating calls from 32-bit code to 16-bit code. An intermediate code translates memory address between platforms. Microsoft has a thunking layer of itself called Win32s, which permit 32-bit Windows application to run on 16-bit Windows. The 64-bit Windows version also provides a thunking layer, WoW64, which permits using 32-bit Windows applications.