What exactly is a reentrant function?

Most of the times, the definition of reentrance is quoted from Wikipedia:

A computer program or routine is
described as reentrant if it can be
safely called again before its
previous invocation has been completed
(i.e it can be safely executed
concurrently). To be reentrant, a
computer program or routine:

  1. Must hold no static (or global)
    non-constant data.
  2. Must not return the address to
    static (or global) non-constant
    data.
  3. Must work only on the data provided
    to it by the caller.
  4. Must not rely on locks to singleton
    resources.
  5. Must not modify its own code (unless
    executing in its own unique thread
    storage)
  6. Must not call non-reentrant computer
    programs or routines.

How is safely defined?

If a program can be safely executed concurrently, does it always mean that it is reentrant?

What exactly is the common thread between the six points mentioned that I should keep in mind while checking my code for reentrant capabilities?

Also,

  1. Are all recursive functions reentrant?
  2. Are all thread-safe functions reentrant?
  3. Are all recursive and thread-safe functions reentrant?

While writing this question, one thing comes to mind:
Are the terms like reentrance and thread safety absolute at all i.e. do they have fixed concrete definitions? For, if they are not, this question is not very meaningful.

8 Answers
8

Leave a Comment