What is a 'thunk'? (in the context of re-entrant sort functions, eg: qsort_r)

317 views Asked by At

Having already checked What is a 'thunk'?, I'm still not sure why the extra argument passed to the re-entrant qsort_r would be called thunk.

Eg: (BSD style qsort_r)

void qsort_r(void *base_, size_t nmemb, size_t size, void *thunk,
             int (*compar)(void *, const void *, const void *));

To be clear, I know what the argument is for, and how to use it, just not why its called thunk (in the BSD's qsort_r at least).

1

There are 1 answers

1
AShelly On BEST ANSWER

The name seems to be from the original proposed implementation by Diomidis Spinellis

The implementation shows that it is just used as an opaque data block, passed through qsort_r and back to your compare function.

It seems to somewhat match the 3rd concept in this answer.

a mapping of machine data from one system-specific form to another, usually for compatibility reasons

But really, it just seems like a misleading name. I usually think of thunks as containing blocks of code. In this case it is just a container for context.