Reentrant (subroutine)

Reentrant (subroutine)

A computer program or routine is described as reentrant if it can be safely executed concurrently; that is, the routine can be re-entered while it is already running. To be reentrant, a function:

* Must hold no static (global) non-constant data.
* Must not return the address to static (global) non-constant data.
* Must work only on the data provided to it by the caller.
* Must not rely on locks to singleton resources.
* Must not call non-reentrant functions.

Multiple levels of 'user/object/process priority' and/or multiprocessing usually complicate the control of reentrant code. Also, IO code is usually not reentrant because it relies on shared, singleton resources such as disks.

Reentrancy is a key feature of functional programming.

Examples

In the following piece of C code, neither functions f nor g are reentrant.

int g_var = 1; int f() { g_var = g_var + 2; return g_var; } int g() { return f() + 2; }

In the above, f depends on a global variable "g_var"; thus, if two threads execute it and access g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

These slightly-altered versions are reentrant:

int f(int i) { return i + 2; } int g(int i) { return f(i) + 2; }

Relation to thread-safety

Both concepts of reentrancy and thread-safety relate to the way functions handle resources. However, they are not the same. While the concept of reentrancy can affect the external interface of a function, thread-safety only concerns the implementation of the function and not its external interface.

* In most cases, to make a non-reentrant function reentrant, its external interface must be modified such that all data is provided by the caller of the function.

* To make a thread-unsafe function thread-safe, only the implementation needs to be changed, usually by adding synchronization blocks to protect shared resources from concurrent accesses by different threads.

Therefore, reentrancy is a more fundamental property than thread-safety and by definition, leads to thread-safety: Every reentrant function is thread-safe, however, not every thread-safe function is reentrant.

ee also

*Referential transparency (computer science)

External links

*Article " [http://www-106.ibm.com/developerworks/linux/library/l-reent.html Use reentrant functions for safer signal handling] " by Dipak K Jha
*" [http://publib.boulder.ibm.com/infocenter/systems/topic/com.ibm.aix.genprogc/doc/genprogc/writing_reentrant_thread_safe_code.htm Writing Reentrant and Thread-Safe Code] ," from "AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs", 2nd edition, 1999.


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Reentrant — or re entrant can refer to:*Reentrant (subroutine) *Salients, re entrants and pockets *Reentrant tuning …   Wikipedia

  • Mach (kernel) — Mach is an operating system microkernel developed at Carnegie Mellon University to support operating system research, primarily distributed and parallel computation. It is one of the earliest examples of a microkernel, and still the standard by… …   Wikipedia

  • Recursion — Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. The term is also used more generally to describe a process of repeating objects in a self… …   Wikipedia

  • Call stack — In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This kind of stack is also known as an execution stack, control stack, run time stack, or machine stack, and… …   Wikipedia

  • Michigan Terminal System — (MTS) The MTS welcome screen as seen through a 3270 terminal emulator. Company / developer University of Michigan and 7 other universities in the US, Canada, and the UK …   Wikipedia

  • PIC microcontroller — PIC microcontrollers in DIP and QFN packages …   Wikipedia

  • Thread safety — is a computer programming concept applicable in the context of multi threaded programs. A piece of code is thread safe if it functions correctly during simultaneous execution by multiple threads. In particular, it must satisfy the need for… …   Wikipedia

  • CICS — For the Sudbury, Ontario radio station, see CICS FM. Customer Information Control System (CICS) is a transaction server that runs primarily on IBM mainframe systems under z/OS and z/VSE. CICS is a transaction manager designed for rapid, high… …   Wikipedia

  • Routine — may refer to: * Routine, as a course of normative, standardized actions or procedures that are followed regularly, oftentimes repetitiously; * Choreographed routine, an orchestrated dance involving several performers; * Comedy routine, a comedic… …   Wikipedia

  • Asynchronous System Trap — (AST) refers to a mechanism used in several computer operating systems designed by the former Digital Equipment Corporation (DEC) of Maynard, Massachusetts.Various events within these systems can be optionally signalled back to the user processes …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”