tags:

views:

818

answers:

6
+21  Q: 

What is a '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?

+33  A: 

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

Robert Harvey
Interesting; I usually hear the second form, but I guess it depends what kind of work you do more often
Michael Mrozek
Specifically, related by being automatically generated very short blocks of machine code - even the first case is normally just giving context to a precompiled implementation function.
Simon Buchan
You're one vote away from getting the new "wikibadge". :)
MusiGenesis
Heh, is that badge for finding stuff on WikiPedia?
Bratch
It's a silver, given out for "Answer containing Wikipedia link voted up 25 times for question with at least 3 answers containing Wikipedia links" (from the Badges page). 6 away, I got it wrong.
MusiGenesis
nice copy and paste
hhafez
@hhafez: Thanks, I thought so myself.
Robert Harvey
@Robert, just joking mate, hope you take the humour in good spirit, no offence intended ;)
hhafez
@hhafez: None taken. But you never know with programmers. :)
Robert Harvey
+11  A: 

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

OscarRyz
This was the specific case I was looking for; thanks!
fbrereto
Who'd a thunk it?
Joseph Garvin
@Joseph: Nice. =D
Chris Cooper
+1  A: 

This question has already been asked on SO, see:

http://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.

stimpy77
+3  A: 

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

MusiGenesis
Is that what you thunk?
Duncan
Yes ... Duncan ... I ... did ... thunk ... that. :)
MusiGenesis
+3  A: 

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.

Jerry Coffin
Sounds like the opposite of syntactic sugar to me :)
Laserallan
Syntactic sugar for compilers then? Almost, but not quite, entirely unlike syntactic sugar.
Duncan
+1, nice analogy
kizzx2
+5  A: 

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 compatable, 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 fixup 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.

Chris Dodd