Re-entrant functions

When is a function re-entrant ?

“Robert Love” himself answers the question on Quora 🙂

To quote  him:

When is a function reentrant?

A function is reentrant if it can be invoked while already in the process of executing. That is, a function is reentrant if it can be interrupted in the middle of execution (for example, by a signal or interrupt) and invoked again before the interrupted execution completes.

For example, the following function is not reentrant, because the observed value of the summation depends on when and where the function is interrupted (or, in the case of multithreading, how two or more threads race into the function):

static int sum = 0;
int increment(int i) {
  sum += i;
  return sum;
}

We can make this function reentrant by making the sum not a global variable and instead requiring the caller to maintain it:

int increment(int sum, int i) {
    return sum + i;
}

ctim(),gmtime(),and strtok()are examples of functions in Standard C that are (or at least often are) not reentrant. Later versions of the standard have created reentrant versions, for example strtok_r() with the primary difference that the caller passes in storage instead of the function maintaining it globally, as above.

How does that relate to it being thread-safe?

The term reentrancy predates threads and is often used only in a single threaded context, for example when discussing if a function is signal or interrupt-safe. Reentrancy and thread-safety are not the same and should not be confused.

Although a reentrant function is more likely to be thread-safe all else equal, a reentrant function need not be thread-safe, and a thread-safe function need not be reentrant. For example, reentrant functions that operate on the same data (same inputs) may not be thread-safe. Similarly, a thread-safe function that achieves safety through a mutex is potentially not reentrant, as the interruption could deadlock on the already-acquired mutex.

To summarize, we might define reentrancy and thread safety in similar terms, but with slightly different semantics:

A reentrant function can be invoked, interrupted, and re-invoked. Such a function can be invoked simultaneously by multiple threads if and only if each invocation references or provides unique data and inputs.

A thread-safe function can be invoked simultaneously by multiple threads, even if each invocation references or provides the same data or input, as all access is serialized.

Writing reentrant code.

Writing code that is reentrant is not hard. The styles and patterns adapted by programmers since the mid-1990s are largely reentrant. Indeed, most programmers today balk at solutions to problems such as the approach adopted by

strtok()

, even if not concerned with reentrancy or thread safety.

Basic guidelines:

  • Do not access mutable global or function-static variables.

  • Do not self-modify code.

  • Do not invoke another function that is itself non-reentrant.

Reference : https://www.quora.com/When-is-a-function-reentrant-How-does-that-relate-to-it-being-thread-safe